store.js代码配置
state: {
userInfo: {
ADID: '',
Token: '',
},
mutations: {
//设置userInfo
SetUserInfo(state, data) {
// console.log(state)
for (let i in data) {
state.userInfo[i] = data[i]
}
},
}
子页面A的代码
computed: mapState({
//使用vuex及时更新数据
ADID() {
return this.$store.state.userInfo.ADID;
},
info() {
return this.$store.state.userInfo;
},
}),
子页面B的部分代码
mounted() {
this.GetUserInfo();
}
methods: {
GetUserInfo() {
return request({
})
.then(res=>{
this.$store.commit("SetUserInfo", res.data);
})
}
}
备注:(最开始我是在子页面的data初始值设置了ADID:this.store.state.userInfo.ADID。但是这样会导致页面没有及时渲染。查了一下不能直接初始值为this.store。第一种方法就是在页面中直接使用this.store.state.userInfo.ADID**.第二种方法**就是使用mapState。倒是解决了问题。但是还有一个疑问就是我在mounted中log这个this.$store.state.userInfo.ADID还是为空。不过暂时没用到所以也没想到什么好的方法。如果有人懂为什么可以告诉我一下谢谢)