【问题标题】:How pass parameters to Vuex actions from Nuxt component如何将参数从 Nuxt 组件传递给 Vuex 操作
【发布时间】:2019-06-14 05:22:31
【问题描述】:

我正在使用 Nuxt.js 和 Vuex,我担心如何将参数从组件传递到 nuxt 中的 vuex。

组件:

<div v-for="item in items" @click="getDetails(item)">
    {{item.name}}
</div>

 export default {
        methods: {
            voteDetail(item) {
                this.$store.dispatch('voteDetail');
            }
        }
    }

在 Vuex 商店中:

async voteDetail(item) {
     console.log(item)
     }

这里出了点问题:)

【问题讨论】:

    标签: vuex nuxt.js


    【解决方案1】:

    因为您需要将其传递到您的操作中。如果你不将它传递给参数调用,它怎么知道你传递了它?

    this.$store.dispatch('voteDetail', item);
    

    在您的操作中,第一个参数也是上下文,而不是项目。请阅读docs

    async voteDetail(context, item) {
         console.log(item)
         }
    

    【讨论】:

    • 但是当我 console.log {dispatch: ƒ, commit: ƒ, getters: {...}, state: {...}, rootGetters: {...}, ...} commit: ƒ boundCommit(type, payload, options) dispatch: ƒ boundDispatch(type, payload) getter: {…} rootGetters: {…} rootState: {…} state: {…}
    猜你喜欢
    • 2019-06-11
    • 2021-12-23
    • 2020-07-26
    • 2020-08-14
    • 2018-10-04
    • 2021-04-23
    • 1970-01-01
    • 2019-02-11
    • 2020-08-09
    相关资源
    最近更新 更多