(vue)Vue前台警告You may have an infinite update loop in watcher with expression

原因:循环的数组设置了全局(和我的特殊数组有关)

修改前:

    //遍历标签
    changeCompare(val, keyword) { 
        this.textList.forEach((item1) => {
          this.color = item1.color; //区别
          this.contentTest = item1.content; //区别
          this.contentTest.forEach((item) => {
            keyword = item;
            val = val + "";
            if (val.indexOf(item) !== -1 && item !== "") {
              val = val.replace(
                new RegExp(item, "g"),
                `<font color='#000' style="
        background-color: ${this.color};
        border-radius: 5px;
        padding: 2px 4px; margin:2px;">${item}</font>`
              );
            }
          });
        });
        return val; 
    },

修改后:

    //遍历标签
    changeCompare(val, keyword) { 
        this.textList.forEach((item1) => { 
          var color = item1.color; //区别
          var contentTest = item1.content; //区别
          contentTest.forEach((item) => {
            keyword = item;
            val = val + "";
            if (val.indexOf(item) !== -1 && item !== "") {
              val = val.replace(
                new RegExp(item, "g"),
                `<font color='#000' style="
        background-color: ${color};
        border-radius: 5px;
        padding: 2px 4px; margin:2px;">${item}</font>`
              );
            }
          });
        });
        return val; 
    },

解决参考:
https://blog.csdn.net/qq_42569573/article/details/106691756

https://blog.csdn.net/weixin_46820017/article/details/122229053

https://www.jianshu.com/p/a51fb3694673?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes

Logo

前往低代码交流专区

更多推荐