从vuex中获取到数据,在什么时机赋值给本组件数据?
1、在created阶段赋值给本地数据
userForm:{
Name: '',
Description: '',
Street: '',
City: '',
House: '',
Province: '',
Country: '',
PostCode: ''
},
...mapState("dealWork", {
CustomerInfo: "CustomerInfo"
})
Object.assgin(this.userForm, this.CustomerInfo)
2、利用计算属性与监听属性
userForm:{
Name: '',
Description: '',
Street: '',
City: '',
House: '',
Province: '',
Country: '',
PostCode: ''
},
...mapState("dealWork", {
CustomerInfo: "CustomerInfo"
})
watch: {
CustomerInfo: {
handler: function (val) {
this.userForm = val;
},
deep: true
}
},
3、在某些情况下是需要两种都去写的:
从vuex中第一次取数据然后赋值给本组件数据,必须要用到watch, 但是多组件切换显示(只用watch)的话发现切换回来,就没有数据了。所以还需要在created阶段将vuex中数据赋值给本地
|