【发布时间】:2018-09-02 09:03:15
【问题描述】:
我有一个保存布尔值的 vue.js 存储,我需要检测它何时在组件内发生变化。当 userEnabled 更改时,我需要调用组件内的方法。我不需要计算新值。
我的问题是我应该为此使用 'watch' 还是有更有效的方法?如果是这样,我是否应该在“已安装”中配置手表,而取消手表呢?
store.js
import Vue from 'vue';
import Vuex from 'vuex';
Vue.use(Vuex);
export const store = new Vuex.Store({
state : {
userEnabled: false,
}
});
user.vue
<template>
<div>
<div id="user"></div>
</div>
</template>
<script>
export default {
watch: {
'this.$store.userEnabled': function() {
console.log('test');
}
},
mounted: function() {
this.$store.watch( state => state.userEnabled, (newValue, oldValue) => {
// call function!
});
}
}
</script>
【问题讨论】: