为什么方法一的这种写法能监听到popupMsg的变化,但是方法二不能监听的popupMsg的变化?
方法一
export default {
data() {
return {
popupMsg: ''
}
},
methods: {
getData() {
this.$store.dispatch('userGroup/getManualTags', param).then(res => {
this.popupMsg = this.$store.state.userGroup.manualTags.popupMsg;
})
}
},
mounted() {
this.getData();
},
watch: {
popupMsg(curVal, oldVal) {
console.log(curVal);
console.log(oldVal);
this.$message.error(curVal);
}
}
}
方法二
export default {
data() {
return {
}
},
computed: {
popupMsg() {
return this.$store.state.userGroup.manualTags.popupMsg;
}
},
methods: {
getData() {
this.$store.dispatch('userGroup/getManualTags', param);
}
},
mounted() {
this.getData();
},
watch: {
popupMsg(curVal, oldVal) {
console.log(curVal);
console.log(oldVal);
this.$message.error(curVal);
}
}
}
并不是第二种方法有问题,而是第二种写法如果你只对popupMsg赋值了一次,watch是不会对第一次赋值做出任何反应的;this.$store.state.userGroup.manualTags.popupMsg的初始值是什么?