【问题标题】:vuejs 2 how to watch store values from vuex when params are usedvuejs 2 如何在使用参数时从 vuex 中查看存储值
【发布时间】:2018-08-25 10:58:53
【问题描述】:

如何在使用参数时观察存储值的变化?我通常会通过 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
        },
});

vue-component.vue:

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

    watch: {
        results(){ // How to pass the param ??
            // my callback
        }

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

【问题讨论】:

    标签: vue.js vuejs2 vuex


    【解决方案1】:

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

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

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

    computed: {
      ...mapGetters([
        'results',
      ]),
      scan() {
    
      },
    
      mutatedResults() {
        return this.results(this.scan);
      },
    
      watch: {
       mutatedResults() {
    
       }
      }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2016-12-16
      • 1970-01-01
      • 2017-09-02
      • 2020-08-24
      • 1970-01-01
      • 2018-11-10
      • 2020-11-17
      • 2021-07-06
      • 1970-01-01
      相关资源
      最近更新 更多