Vuejs - vuex 延迟加载

mar*_*nho 3 vue.js vue-router vuex vuejs2

我有一个相当大的 VueJS SPA,我只想使用延迟加载在某些路由上加载 vuex 模块。

我正在关注这篇文章来重现此内容 - https://alexjoverm.github.io/2017/07/16/Lazy-load-in-Vue-using-Webpack-s-code-splitting/

然而,这给了我一个 vuex 错误。

文件夹结构

root
  |- store
  | |- modules
  | | |- auth.js
  | | |- module2.js
  | |- store.js
  |- app.js
Run Code Online (Sandbox Code Playgroud)

auth.js

const state = {
    var1: {},
    var2: false
}

const mutations = {
    'MUTATION_1'(state, obj) {
        // logic
    }
}

const actions = {
    action1({commit}) {
       // logic
    }
}

const getters = {
    var1: state => state.var1,
    var2: state => state.var2
}

export default {
    state,
    mutations,
    actions,
    getters
}
Run Code Online (Sandbox Code Playgroud)

store.js -

import Vue from 'vue';
import Vuex from 'vuex';

Vue.use(Vuex);
const store = new Vuex.Store();

import('./modules/auth.js').then(auth => {
    store.registerModule('/login', auth);
});

export default store;
Run Code Online (Sandbox Code Playgroud)

应用程序.js -

import Vue from 'vue';
import store from './store/store';
import VueRouter from 'vue-router';
import { routes } from './routes/routes';

// vue-router config
Vue.use(VueRouter);

const router = new VueRouter({
    mode: 'history',
    routes
});

const app = new Vue({
    el: '#app',
    store,
    router
});
Run Code Online (Sandbox Code Playgroud)

错误 -

[vuex] unknown getter: var1
Run Code Online (Sandbox Code Playgroud)

有什么建议吗?

kfe*_*v91 5

store.registerModule('/login', auth);
Run Code Online (Sandbox Code Playgroud)

上面的代码使用soauth的命名空间注册模块,login以便从存储中访问其状态、getter、突变和操作,您必须为所有这些添加前缀 path login/。您收到错误是因为您可能store.getters.var1在应该调用 的时候尝试了store.getters['login/var1']