在重构项目框架的时候,我发现了导航的二级标题是用本地存储的方法进行传值的,之前到是没问题,因为标题也不需要事实更新,只需要mounted 生命周期 去下值就行,但是重构要求实事更新,这。。。这代码是谁写的,为啥不用 vuex?! 打开提交记录,。。好吧 是我写的QAQ~
data() {
return {
system:JSON.parse(localStorage.getItem("system")), // 取值
isSettings: localStorage.getItem("isSettings"),
pSystem:JSON.parse(localStorage.getItem("pSystem")) // 取值
};
},
===================
localStorage.setItem("system", JSON.stringify(item)); 存值。
犹豫半天,我还是不想用 vuex 因为项目的vuex 模块明确,基本不动了,
主要还是懒的写,我明白我之前为啥不用了,,,
正没头绪的时候 我瞄到了地址懒
id 是随着点击跟着变化的。我为啥不在 导航上加一个事件监听,当路由发生变化时,再重新读一遍local 数据就行了,
于是:
watch:{
"$route.query":{
handler(val){
// 防止其他页面受影响 (机智如我)
if (this.$route.path == '/system-children') {
this.pSystem = JSON.parse(localStorage.getItem("pSystem"))
}
}
}
},
搞定。