【发布时间】:2018-01-10 21:15:58
【问题描述】:
我正在使用 Vue.js 和 Vuex。
我想watchstore 的值(我将在本文的最后分享我的示例代码)。
但是我遇到了错误“错误:[vuex] store.watch 只接受一个函数。”
在这个网站上,我发现了如何使用“单一”商店。 https://codepen.io/CodinCat/pen/PpNvYr
但是,就我而言,我想使用“两个”商店。 你有什么办法解决这个问题吗?
●index.js
'use strict';
import Vue from 'vue';
import Vuex from 'vuex';
import {test1Store} from './modules/test1.js';
import {test2Store} from './modules/test2.js';
Vue.use(Vuex);
export const store = new Vuex.Store({
modules: {
test1: test1Store,
test2: tes21Store,
}
});
●test1.js
'use strict';
import Vue from 'vue';
import Vuex from 'vuex';
Vue.use(Vuex);
export const checkerStore = {
namespaced: true,
state: {
count: 1
},
getters: {
getCount(state){
return state.count;
}
}
};
export default {test1};
●test.vue
<template>
{{ $store.state.couunt }}
</template>
<script>
import {store} from './store/index.js';
export default {
data: function () {
return {
}
},
store: store,
methods: {
},
mounted() {
setInterval(() => { this.$store.state.count++ }, 1000);
this.$store.watch(this.$store.getters['test1/getCount'], n => {
console.log('watched: ', n)
})
}
}
}
</script>
【问题讨论】: