vuejs 2 如何在使用参数时从 vuex 中观察存储值

Mke*_*key 2 vue.js vuex vuejs2

使用参数时如何观察存储值的变化?我通常会通过 getter 来做到这一点,但我的 getter 接受一个参数,这使得它变得棘手,因为我未能找到有关此场景的文档或堆栈 Q/A。

(出于演示原因,代码被最小化)我的 store.js :

import Vue from 'vue';
import Vuex from 'vuex';

Vue.use(Vuex);

let report = {
    results: [],
};
export const store = new Vuex.Store({
    state: {   
        broken: Object.assign({}, report),    
    },
     results: (state) => (scan) => {
            return state[scan].results
        },
});
Run Code Online (Sandbox Code Playgroud)

vue-component.vue :

computed: {
        ...mapGetters([ 
         'results',
        ]),

    watch: {
        results(){ // How to pass the param ??
            // my callback
        }
Run Code Online (Sandbox Code Playgroud)

所以基本上我想找出如何传递参数以便我的手表工作。

Yu *_*ang 7

在我看来,你的问题没有直接的解决方案。
起初,对于 watch 函数,它只接受两个参数,newValue 和 oldValue,所以没有办法传递你的 scan 参数。
此外,您的结果属性在计算中,只返回一个函数,如果您观看该函数,它将永远不会被触发。

我建议您将 getter 从嵌套函数更改为简单函数。

但是如果你真的想这样做,你应该创建一个桥计算属性

computed: {
  ...mapGetters([
    'results',
  ]),
  scan() {

  },

  mutatedResults() {
    return this.results(this.scan);
  },

  watch: {
   mutatedResults() {

   }
  }

}
Run Code Online (Sandbox Code Playgroud)