Error in callback for watcher "function () { return this._data.$$state }": "Error: [vuex] do not mutate vuex store state outside mutation handlers."
vue.esm.js:1897 Error: [vuex] do not mutate vuex store state outside mutation handlers.
Vuex 中,所有的状态变更应该通过 mutation 来进行,以确保状态变更的可追踪性和可预测性。
当直接修改 Vuex 的状态,比如通过引用直接修改一个数组或对象,Vue 无法跟踪这种改变的来源,导致可能的数据流问题和组件渲染错误。
static translate(msg: any, subStr: string) {
if (msg && msg.content.link_preview && msg.content.link_preview.previews && Array.isArray(msg.content.link_preview.previews)) {
let reg = /((http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&():/~\+#]*[\w\-\@?^=%&()/~\+#])?)|(((25[0-5]|2[0-4]\d|[01]?\d\d?)($|(?!\.$)\.)){4})|((25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9]):\d{0,5})/gi;
let link_preview = [...msg.content.link_preview.previews];
//为了解决链接是包含关系 需要先将preview按长度排序
link_preview=link_preview.sort((b:any,a:any)=>{return a.link.length - b.link.length})
subStr = subStr.replace(reg, function(str: any, $1: any) {
let i = link_preview.findIndex((preview: any) => {
return $1 == preview.link;
});
if (i != -1 && link_preview[i].title) {
return `「${link_preview[i].title}」`;
} else {
return $1;
}
});
}
return subStr;
}
let link_preview = msg.content.link_preview.previews;
改成let link_preview = [...msg.content.link_preview.previews];