有没有办法将参数传递给 vuex
store
的 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>
但在 vuex 中,第一个参数是 state
,第二个是其他 getters
。可能吗?
最佳答案
ES6 箭头函数在这里也能很好地工作。例如,假设您正在商店中寻找特定的“东西”。
new Vuex.Store({
getters: {
someMethod: (state) => (id) => {
return state.things.find(thing => thing.id === id)
}
},
})
这是通过 Vuex documentation 的另一个示例
关于javascript - 带参数的 vuexjs getter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41503527/