gpt4 book ai didi

javascript - 如何将多个参数传递给 vuex getter?

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:20:39 26 4
gpt4 key购买 nike

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

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

with withFilters 在商店中定义的 setter/getter 。但是我如何在商店中获取传递给 withFilters 的参数并相应地定义这个 getter?对于 1-argument 函数,我会简单地做类似的事情

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

例如:

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

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

最佳答案

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

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

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

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

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

// Parentheses are optional when there's only one parameter name:
(singleParam) => { statements }
singleParam => { statements }

关于javascript - 如何将多个参数传递给 vuex getter?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52194840/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com