我同时使用vuex和vuejs 2。
我是vuex的新手,我想看一个商店变量的变化。
我想在我的vue组件中添加手表功能
这是我目前所拥有的:
import Vue from 'vue';
import {
MY_STATE,
} from './../../mutation-types';
export default {
[MY_STATE](state, token) {
state.my_state = token;
},
};
我想知道my_state是否有任何变化
我怎么看店。My_state在我的vuejs组件?
这是为所有的人,不能解决他们的问题与getter,实际上真的需要一个监视器,例如,与非Vue第三方的东西(见Vue监视器何时使用监视器)。
Vue组件的监视器和计算值也都适用于计算值。所以vuex也没有什么不同:
import { mapState } from 'vuex';
export default {
computed: {
...mapState(['somestate']),
someComputedLocalState() {
// is triggered whenever the store state changes
return this.somestate + ' works too';
}
},
watch: {
somestate(val, oldVal) {
// is triggered whenever the store state changes
console.log('do stuff', val, oldVal);
}
}
}
如果只是结合本地和全局状态,mapState的文档也提供了一个例子:
computed: {
...mapState({
// to access local state with `this`, a normal function must be used
countPlusLocalState (state) {
return state.count + this.localCount
}
}
})
这是为所有的人,不能解决他们的问题与getter,实际上真的需要一个监视器,例如,与非Vue第三方的东西(见Vue监视器何时使用监视器)。
Vue组件的监视器和计算值也都适用于计算值。所以vuex也没有什么不同:
import { mapState } from 'vuex';
export default {
computed: {
...mapState(['somestate']),
someComputedLocalState() {
// is triggered whenever the store state changes
return this.somestate + ' works too';
}
},
watch: {
somestate(val, oldVal) {
// is triggered whenever the store state changes
console.log('do stuff', val, oldVal);
}
}
}
如果只是结合本地和全局状态,mapState的文档也提供了一个例子:
computed: {
...mapState({
// to access local state with `this`, a normal function must be used
countPlusLocalState (state) {
return state.count + this.localCount
}
}
})
比如说,你有一篮水果,
每次你从篮子里添加或取出一个水果,你
想要(1)显示有关水果数量的信息,但你也(2)想要以某种奇特的方式通知水果的数量…
fruit-count-component.vue
<template>
<!-- We meet our first objective (1) by simply -->
<!-- binding to the count property. -->
<p>Fruits: {{ count }}</p>
</template>
<script>
import basket from '../resources/fruit-basket'
export default () {
computed: {
count () {
return basket.state.fruits.length
// Or return basket.getters.fruitsCount
// (depends on your design decisions).
}
},
watch: {
count (newCount, oldCount) {
// Our fancy notification (2).
console.log(`We have ${newCount} fruits now, yay!`)
}
}
}
</script>
请注意,监视对象中的函数名必须与计算对象中的函数名匹配。在上面的例子中,名字是count。
被监视属性的新值和旧值将作为参数传递到监视回调(count函数)。
篮子商店可以是这样的:
fruit-basket.js
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
const basket = new Vuex.Store({
state: {
fruits: []
},
getters: {
fruitsCount (state) {
return state.fruits.length
}
}
// Obviously you would need some mutations and actions,
// but to make example cleaner I'll skip this part.
})
export default basket
你可以在以下资源中阅读更多:
计算属性和监视器
API文档:计算
API文档:观察
如上所述,直接在商店中观察变化并不是一个好主意
但在一些非常罕见的情况下,它可能对某人有用,所以我将留下这个答案。其他情况请参见@gabriel-robert answer
你可以通过州政府,$watch。将此添加到组件中创建的(或需要执行此方法的地方)方法中
this.$store.watch(
function (state) {
return state.my_state;
},
function () {
//do something on data change
},
{
deep: true //add this if u need to watch object properties change etc.
}
);
详情:https://vuex.vuejs.org/api/#watch
如果您只是想监视一个状态属性,然后根据该属性的变化在组件中进行相应的操作,请参阅下面的示例。
在store.js:
export const state = () => ({
isClosed: false
})
export const mutations = {
closeWindow(state, payload) {
state.isClosed = payload
}
}
在这个场景中,我创建了一个布尔状态属性,我将在应用程序的不同地方更改,如下所示:
this.$store.commit('closeWindow', true)
现在,如果我需要在其他组件中监视状态属性,然后更改本地属性,我会在挂载的钩子中写入以下内容:
mounted() {
this.$store.watch(
state => state.isClosed,
(value) => {
if (value) { this.localProperty = 'edit' }
}
)
}
首先,我在状态属性上设置了一个监视器,然后在回调函数中,我使用该属性的值来更改localProperty。
我希望这能有所帮助!
正如Gabriel所说,观察商店变化的最佳方法是使用mapGetters。
但在某些情况下,你不能通过mapGetters来实现,例如,你想从store中获取一些东西,使用parameter:
getters: {
getTodoById: (state, getters) => (id) => {
return state.todos.find(todo => todo.id === id)
}
}
在这种情况下,你不能使用mapGetters。你可以试着这样做:
computed: {
todoById() {
return this.$store.getters.getTodoById(this.id)
}
}
但不幸的是,todoById仅在以下情况下才会更新。Id改变了
如果你想更新你的组件在这种情况下使用。$store。腕表解决方案由龚提供。或者有意识地处理组件并更新它。当需要更新todoById时,使用todoById。