前缀或范围Vuex mapActions和mapGetters以避免冲突

问题描述 投票:0回答:1

我正在使用带有动作和getter的user.module的Vuex商店,我在我的组件中导入主题如下:

<template>
     ...

     <login-form v-if="!isAuthenticated"/>

     ...
</template>

<script>
    import { mapActions, mapGetters } from 'vuex'

    export default { 

        ...

        computed :{
            ...mapGetters('user', ['isAuthenticated', 'isProcessing']),
        },
        methods:{
            ...mapActions('user', ['getUser']),
        }

        ...
    }
</script>

上面的脚本工作得很好,我想要的是范围,命名空间或添加前缀到我的动作和getter以避免与我的组件中的其他方法冲突,像user.isAuthenticated,我尝试了这些组合,但没有一个主题做了工作:

...mapActions('user', ['user/getUser'])
...mapActions('user', ['user.getUser'])
...mapActions('user/user', ['getUser'])
...mapActions('user.user', ['getUser'])

提前致谢。

vue.js vuejs2 vuex vuex-modules
1个回答
2
投票

您必须使用对象语法将操作映射到组件中的不同名称:

...mapActions('user', {
  prefix_getUser: 'getUser',  // this.prefix_getUser -> user/getUser action
  prefix_getFoo: 'getFoo',    // this.prefix_getFoo  -> user/getFoo action
})

这在docs中有描述。

© www.soinside.com 2019 - 2024. All rights reserved.