介绍三种方法,其实这三种也没什么太大区别,使用的是cli3脚手架
第一种
也是最简单的一种
watch: {
'$store.state.test': function () {
//你需要执行的代码
}
}
第二种
computed: {
test() {
return this.$store.state.test
},
},
watch: {
test: function(newVal, oldVal) {
//要执行的代码
},
},
第三种
import {mapGetters} from 'vuex'
export default {
computed: {
...mapGetters([
'test'
])
},
watch: {
test: function(newVal, oldVal) {
//要执行的代码
},
},
}