简体   繁体   English

无法在我的 Vue 组件中捕获 Vuex 状态变化

[英]Can not catch Vuex state change in my Vue component

I have a Vuex store where I have a getter which works correctly and I can see the changes on the state.我有一个 Vuex 商店,我有一个可以正常工作的 getter,我可以看到状态的变化。 But if I call this getter as computed property in component it does not work.但是,如果我将此 getter 称为组件中的计算属性,则它不起作用。 The value is still the same.价值还是一样的。

The store code looks like:商店代码如下所示:

mutations: {
  UPDATE_SERVER_FILTERS(state, payload) {
    this._vm.$set(state, 'serverFilters', payload);
    //state.serverFilters = payload;  // Both patterns work
  },
  getters: {
    serverFilters(state) {
      return state.serverFilters;  // This works fine
    }
  }
}

The component code:组件代码:

computed: {
  serverFilters() {
    return this.$store.getters[this.storeName + '/serverFilters'];
  },
}

Here is JSFiddle example https://jsfiddle.net/camo/je0gw9t3/4/ which works fine.这是 JSFiddle 示例https://jsfiddle.net/camo/je0gw9t3/4/工作正常。 And it is a problem cause in my project it does not work.这是一个问题,因为在我的项目中它不起作用。 I am prepared to die...我已经做好了死的准备……

How can I solve it?我该如何解决?

In the most bottom part:在最底部:

new Vue({
  store,
  el: '#example',
  data() {
    return {};
  },
  computed: {},
  methods: {
    changeFilters() {
      this.$store.dispatch(this.storeName + '/updateFilters');
      //                   ^^^^^^^^^^^^^^ there is no storeName 
    },
  },
});

The changeFilters method. changeFilters 方法。 You are using this.storeName , but there is no this.storeName !您正在使用this.storeName ,但没​​有this.storeName Just like the Child component, add storeName: 'a' to the data() then it should work.就像 Child 组件一样,将storeName: 'a'添加到data()然后它应该可以工作。

https://jsfiddle.net/4yfv3w87/ https://jsfiddle.net/4yfv3w87/


Here is the debug process for your reference:以下是调试过程供您参考:

First open the Vue Devtools and switch to the timeline tab.首先打开 Vue Devtools 并切换到时间线选项卡。 And just click the button, you will see that there is no action is being fired.只需单击按钮,您就会看到没有触发任何操作。 So the problem must be the one who dispatches the action.所以问题一定是派出行动的那个人。 And then you will notice that the root component doesn't have a storeName .然后你会注意到根组件没有storeName

So don't panic, just try to trace the code.所以不要惊慌,只需尝试跟踪代码即可。 It will only take a few minutes to find out the issue!只需几分钟即可找出问题!

Computed properties might have problem to make an observer reference from returned value out of function.计算属性可能无法从函数的返回值中进行观察者引用。 Instead of chaining getters and computed properties, why you don't use just getters or computed properties ?为什么不使用 getter 或计算属性,而不是链接 getter 和计算属性? In my opinion, it's a bad practice to use them both, and I can't imagine a situation you need it.在我看来,同时使用它们是一种不好的做法,我无法想象你需要它的情况。 So if you need filter operations in many components, just make a getter and use getter in components instead of computed properties.因此,如果您需要在许多组件中进行过滤操作,只需制作一个 getter 并在组件中使用 getter 而不是计算属性。

If you really want to chain them, try this:如果你真的想链接它们,试试这个:

new Vue({
    store,
    el: '#example',
    data() {
        return {
        storeName: 'a'
      }
    },
    computed: {
      filters() {
          get() {
              return this.$store.getters[`${this.storeName}/getFilters`];
          }
          set(newValue) {
              this.$store.dispatch(this.storeName + '/updateFilters');
          }
      },
    },
})

Comment please if someone check it.如果有人检查,请发表评论。 I don't know are it works.我不知道它是否有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM