【问题标题】:How to pass multiple parameters to vuex getters?如何将多个参数传递给 vuex getter?
【发布时间】:2019-02-11 04:08:12
【问题描述】:

在一个 vue.js 组件中(使用 vuex),我有:

computed: {...
    filtered () {
      return this.withFilters(obj1, obj2) 

使用withFilters 在商店中定义一个吸气剂。但是如何在商店中获取传递给withFilters 的参数并相应地定义这个getter?对于一个 1 参数函数,我会简单地做类似的事情

withFilter: state => obj1 => { ...

例如:

withFilter: state => obj1 => {
 for (var k in obj1) {
  obj1[k] !== null ? console.log('Not null!') : console.log('Null!')
 }
}

不确定如何使用 2 参数函数。

【问题讨论】:

    标签: javascript vuejs2 vuex


    【解决方案1】:

    您只需要使用多个参数定义getter 返回的函数。例如

    getters: {
      withFilter (state) {
        return (obj1, obj2) => {
          // your code here
        }
      }
    }
    

    你可以完全用箭头函数来写这个,但我认为它看起来很乱(个人意见)

    withFilter: (state) => (obj1, obj2) => {
      // your code here
    }
    

    请参阅有关 ES6 箭头函数和参数括号here...的文档

    // 当只有一个参数名时,括号是可选的:
    (singleParam) => { 语句 }
    singleParam => { 语句 }

    【讨论】:

    • 非常感谢菲尔。我完全尝试了您建议的第二种语法,但由于某种原因(在深夜这样做,所以我的状态不是很好)它没有工作,也没有引发错误。现在将尝试您的建议,我知道语法是正确的。
    猜你喜欢
    • 2020-08-09
    • 2020-07-22
    • 2021-03-26
    • 2021-04-23
    • 2023-04-03
    • 2011-08-08
    • 2019-06-11
    • 2021-12-11
    • 1970-01-01
    相关资源
    最近更新 更多