【问题标题】:Why namespased: false does not work for me为什么命名:false 对我不起作用
【发布时间】:2020-07-06 11:51:36
【问题描述】:

我见过类似的问题。但是我失去了一些东西,无法理解为什么它在我的情况下不起作用。 我有以下 vuex 模块:

const root = {
    namespaced: false,
    state: {
        advertisersOptions: [],
        customersTypesOptions: [],
       // ... more state props are here ...
    },
    mutations: {
        setIsChoiceAllApps(state, isChoiceAllApps) {
            state.isChoiceAllApps = isChoiceAllApps;
        },

       // and so on 
    },
    getters: {
        getErrorMessages: state => {
            return state.errorMessages;
        },
        // and so on
    },
    actions: {},
};

export default root

我像这样创建我的商店:

import Vue from 'vue'
import * as Vuex from 'vuex'
import root from './modules/root'

Vue.use(Vuex);

export const store = new Vuex.Store({
    modules: {
         root
    }
});

我将它包含在app.js 文件中:

const app = new Vue({
    el: '#app',
    store,
    components: {
        Multiselect
    },
});

所以,我想在...mapState(['...']) 之后查看我的 vuex 属性。我将向您展示组件:

export default {
    data() {
        return {
            choiceAllApplications: false
        }
    },
    methods: {
        ...mapActions(['updateApplicationsAction']),
        disposeAllApps: function () {
            this.$store.commit("setApplications", []);
        },
    },
    computed: {
        ...mapState([
            "advertisersOptions",
            "advertisersSelected",
            // and so on are here
        ]),
    },

如您所见,我使用namespaced: false,。我只想在全局范围内查看我的属性。但它不起作用。我将在mounted() 中创建console.log,这是转储:

如您所见,模块名称 (root) 在这里。我忘记了什么?

【问题讨论】:

    标签: javascript vue.js vuex


    【解决方案1】:

    你没有忘记任何东西,它总是这样 - 根据 Vuex docs,只有 getters, setters and actions 受命名空间的影响(而且,顺便说一句,即使它们是命名空间,它们仍然会出现在全局对象中但要像moduleName/action - user/getUserName 而不是像user.getUserName 这样的对象内部)。

    但是 state 始终是每个模块的 - 因此,无论是否命名空间,它都将位于全局状态对象中它自己的属性中。

    【讨论】:

      猜你喜欢
      • 2015-05-20
      • 1970-01-01
      • 1970-01-01
      • 2016-11-18
      • 1970-01-01
      • 2010-10-08
      • 2016-02-27
      • 2019-01-13
      • 2015-09-07
      相关资源
      最近更新 更多