vue-定时器-element的notify提示框-自定义样式

本文介绍了如何在Vue.js项目中利用定时器实现每2秒向后端请求异常日志,并结合Element UI的notify功能,创建一个自定义样式的提示框。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

系统要实现一个简单的异常日志提示框,决定使用定时器,每隔2秒向后端发送请求
效果图:
在这里插入图片描述
登录后,开启定时器

data() {
    return {
      isExistNo: false,
      timer:null, //定时器名称
    };
  },
created() {
    const _this = this;
    this.timer = window.setInterval(() => {
        _this.$api.post(_this.$lesUiPath.msgAlert).then(result => {
          if (result !== "") {
            if (_this.isExistNo) {
              // console.log("已存在提示框...");
            } else {
              _this.isShowNotify(result);
              _this.isExistNo = true;
            }
          }
        });
    }, 2000);
    // 通过$once来监听定时器
    // 在beforeDestroy钩子触发时清除定时器
    this.$once("hook:beforeDestroy", () => {
      window.clearInterval(this.timer);
      this.timer = null;
      // console.log("离开页面,定时器销毁...")
    });
  },
  methods: {
    isShowNotify(result) {
      this.$notify({
        title: "提示",
        type: "warning",
        dangerouslyUseHTMLString: true,
        onClose: () => {
          // console.log("点击关闭...");
          this.isExistNo = false;
        },
        message: this.$createElement("div", null, [
          this.$createElement("div", null, [
            this.$createElement("span", null, result)
          ]),
          this.$createElement("div", null, [
            this.$createElement(
              "button",
              {
                style: {
                  padding: "8px 16px",
                  margin: "10px 12px 0px 2px",
                  textAlign: "center",
                  textDecoration: "none",
                  display: "inline-block",
                  webkitTransitionDuration: "0.4s",
                  transitionDuration: "0.4s",
                  cursor: "pointer",
                  backgroundColor: "#ea8f28",
                  color: "white",
                  // border: '2px solid ##ea8f28',
                  borderRadius: "6px"
                },
                on: {
                  click: () => {
                    this.go(result);
                  },
                  mouseout: function(e) {
                    e.target.style.backgroundColor = "#ea8f28";
                    // e.target.style.border = '2px solid #ea8f28';
                  },
                  mouseover: function(e) {
                    e.target.style.backgroundColor = "rgba(234, 143, 40, 0.8)";
                  }
                }
              },
              "立即处理"
            )
          ])
        ]),
        duration: 0,
        position: "bottom-right"
      });
    },
    go(data) {
      this.$router.push({ path: "/a/b/c" });
    },
     },
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值