Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vue.js/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 带参数的vuexjs getter_Javascript_Vue.js_Vuejs2_Vuex - Fatal编程技术网

Javascript 带参数的vuexjs getter

Javascript 带参数的vuexjs getter,javascript,vue.js,vuejs2,vuex,Javascript,Vue.js,Vuejs2,Vuex,是否有方法将参数传递到vuex存储的getter中? 比如: new Vuex.Store({ getters: { someMethod(arg){ // return data from store with query on args } } }) 所以在这个组件中,我可以使用 <template> <div> <p>{{someMethod(this.id)}}</p>

是否有方法将参数传递到
vuex
存储的getter中?
比如:

new Vuex.Store({
  getters: {
    someMethod(arg){
       // return data from store with query on args
    }
  }
})
所以在这个组件中,我可以使用

<template>
    <div>
        <p>{{someMethod(this.id)}}</p>
    </div>
</template>
<script lang="ts">
    import { mapGetters } from "vuex"

    export default {
        props: ['id'],
        computed: mapGetters(['someMethod'])
        }
    }
</script>

{{someMethod(this.id)}

从“vuex”导入{mapGetters} 导出默认值{ 道具:['id'], 计算:mapGetters(['someMethod'])) } }

但在vuex中,第一个参数是
state
,第二个参数是other
getter
。有可能吗?

一种方法是:

new Vuex.Store({
  getters: {
    someMethod(state){
      var self = this;
       return function (args) {
          // return data from store with query on args and self as this
       };       
    }
  }
})
但是,getter不接受参数,原因如下所述:

命名约定有点混乱,getter表示可以以任何形式检索状态,但实际上它们是reducer

也许我们应该让还原子成为纯方法。可用于过滤、映射等

然后可以为getter提供任何上下文。类似于computed,但您现在可以在vuex选项中将computed道具与getter组合。这有助于组件的结构

编辑:

实现相同目标的更好方法是使用ES6箭头,如的答案中所述,使用where可以通过从getter返回函数来传递参数:

new Vuex.Store({
  getters: {
    someMethod: (state) => (id) => {
        return state.things.find(thing => thing.id === id)
      }
    };       
  }
})

ES6箭头函数在这里也可以很好地工作。例如,假设您正在商店中寻找特定的“东西”

new Vuex.Store({
  getters: {
    someMethod: (state) => (id) => {
        return state.things.find(thing => thing.id === id)
      }
    };       
  }
})

下面是另一个示例,通过

定义存储getter后,您可以像这样使用MapGetter助手:

new Vuex.Store({
  getters: {
    someMethod(state){
       return (value) => {
          return value;
       }
    }
  }
})
然后从如下组件调用getter:

<script>
    import { mapGetters } from "vuex"

    export default {
     computed: {
     ...mapGetters(['someMethod'])
     },
     mounted() {
       console.log(this.someMethod('hello world')); // then logs "hello world"
     }       
}
</script>

从“vuex”导入{mapGetters}
导出默认值{
计算:{
…映射获取程序(['someMethod'])
},
安装的(){
console.log(this.someMethod('helloworld');//然后记录“helloworld”
}       
}

您可以通过返回函数将参数传递给getter。当您要查询存储区中的数组时,这尤其有用:

getters: {
  // ...
  getTodoById: (state) => (id) => {
    return state.todos.find(todo => todo.id === id)
  }
}
在vue组件中:

store.getters.getTodoById(2) // -> { id: 2, text: '...', done: false }
请注意,通过方法访问的getter将在每次调用它们时运行,并且不会缓存结果


以上答案摘自Vue官方文档:

Perfect。我使用这种技术的目的非常相似。而不是这个:
让something=this.$store.state.things&&this.$store.state.things.thing?this.$store.state.things.thing:null
,我现在可以做
让something=this.$store.getters.getThings('thing')
这应该是公认的答案,因为a)它回答了问题,b)它不会助长不良做法。接受的答案是错误的,应该删除它。当我这样做时,我得到一个错误:不要在变异处理程序之外变异vuex存储状态。我知道它是有效的,我以前做过,但现在我被困在它上面了:/getter/computed变量的主要用途是缓存结果。你知道它在这种特殊情况下是否适用(以及如何适用)?Ta@xpuu请看这个问题,对缓存进行简短的分析:这个问题很有魅力!非常感谢。