问题
最近有个问题就是我想: B页面(组件)想调用 A页面(组件)中的方法;但是两个页面(组件)毫无关联(刷新 A的数据);
解决思路
使用 VueX 定义一个属性 ,然后 在A页面 定义一个计算属性(computed) 再把 vuex 的属性返回给它,
在监听这个计算属性,发生变化就调用你要执行的方法
state
: {
tableStatus
:false
}
mutations
:{
changeStatus(state
,status
) {
state
.tableStatus
= status
;
},
}
computed
: {
status() {
return this.$store
.state
.tableStatus
;
}
},
watch
:{
status() {
this.initData();
}
},
在然后就是 去B 页面定义一个点击事件(举例) 重新给 Vuex中的属性赋值就行了
closePage() {
let status
= this.$store
.state
.tableStatus
;
this.$store
.commit("changeStatus", !status
);
},
如果有更好的方法可以跟我说一声 (留言或者发消息),毕竟不是专业前端 哈哈哈[^1]