pushViewTags(state, route) {
let backPathIndex = state.viewTags.findIndex(item => item.fullPath == router.options.history.state.back)
let target = state.viewTags.find((item) => item.fullPath === route.fullPath)
let isName = route.name
if (!target && isName) {
if (backPathIndex == -1) {
state.viewTags.push(route)
} else {
state.viewTags.splice(backPathIndex + 1, 0, route)
}
}
localStorage.setItem('viewTags', JSON.stringify(state.viewTags)) //报错地方
},
报错JSON.stringify的对象存在循环引用,在将对象进行json序列化的时候就会报错
解决办法:
插件与第三方库
- circular-json
-
circular-json 是一个可以处理 JSON 串中循环引用对象的 npm 包。在前端开发中,有时候会遇到一些复杂的数据结构,其中包含循环引用,而 JSON.stringify() 方法在处理该类数据时会抛出 TypeError 错误。在这种情况下,circular-json 就可以处理了
circular-json 提供了两个主要方法:stringify() 和 parse()。
安装依赖
安装:npm install --save circular-json
上述错误代码修改:
localStorage.setItem('viewTags', CircularJSON.stringify(state.viewTags))