vuex 不加载用 vuex-module-decorators 装饰的模块

vuex not loading module decorated with vuex-module-decorators

我在尝试将带有 vuex-module-decorators 的商店模块加载到初始化程序时出现此错误:

vuex.esm.js?2f62:261 Uncaught TypeError: Cannot read property 'getters' of undefined at eval (vuex.esm.js?2f62:261) at Array.forEach () at assertRawModule (vuex.esm.js?2f62:260) at ModuleCollection.register (vuex.esm.js?2f62:186) at eval (vuex.esm.js?2f62:200) at eval (vuex.esm.js?2f62:75) at Array.forEach () at forEachValue (vuex.esm.js?2f62:75) at ModuleCollection.register (vuex.esm.js?2f62:199) at new ModuleCollection (vuex.esm.js?2f62:160)

index.ts 文件非常简单,在我将模块引入初始化程序之前一切正常:

import Vue from 'vue';
import Vuex from 'vuex';
import { AuthenticationModule, IAuthenticationState } from './modules/authentication';
import VuexPersistence from 'vuex-persist';

Vue.use(Vuex);

export interface IRootState {
  authentication: IAuthenticationState;
}

const vuexLocal = new VuexPersistence({
  storage: window.localStorage,
});

const store = new Vuex.Store<IRootState>({
  modules: {
    authentication: AuthenticationModule, // if this is not here it works but this will mean the vuex-persist will not work
  },
  plugins: [vuexLocal.plugin],
});

export default store;

这是我认为会引发错误的身份验证模块:

import { Action, getModule, Module, Mutation, VuexModule } from 'vuex-module-decorators';
import { Generic422, LoginEmailPost, RegisterPost, TokenRenewPost, User, UserEmailPut, UserPasswordPut, UserPut } from '@/api/ms-authentication/model/models';
import { Api } from '@/services/ApiHelper';
import Auth from '@/services/Auth';
import store from '@/store';

export interface IAuthenticationState {
  user: User;
  authenticated: boolean;
  prompt: {
    login: boolean,
  };
  errorRegister: Generic422;
  errorLogin: Generic422;
}

const moduleName = 'authentication';

@Module({dynamic: true, store, name: moduleName})
class Authentication extends VuexModule implements IAuthenticationState 
{
  public authenticated: boolean = false;
  public errorRegister: Generic422 = {};
  public errorLogin: Generic422 = {};
  public prompt = {
    login: false,
  };
  public user: User = {
    email: '',
    firstName: '',
    lastName: '',
    birthday: '',
    verified: false,
  };
  @Action({commit: 'SET_USER'})
  public async login(data: LoginEmailPost) {
    try {
      const resp = await Api.authenticationApi.v1LoginEmailPost(data);
      Auth.injectAccessJWT(resp.data.tokenAccess.value);
      Auth.injectRenewalJWT(resp.data.tokenRenewal.value);
      return resp.data.user;
    } catch (e) {
      return e.statusCode;
    }
  }
  @Mutation
  public SET_USER(user: User) {
    this.authenticated = true;
    this.user = {...this.user, ...user};
  }
}

export const AuthenticationModule = getModule(Authentication);

我从以下位置获取此设置:https://github.com/calvin008/vue3-admin

我不知道这是错误还是设置问题,但完全卡在这里,因为我打算在此处使用 vuex-persist "rehydrate" 页面重新加载后的商店。

另一种完全不同的用这个库声明商店的方式在这里:https://github.com/eladcandroid/typescript-vuex-example/blob/master/src/components/Profile.vue但是语法似乎会变得非常冗长,当在 vue3-admin 中它完全整齐地在商店中与组件相对.

目前我已将所有状态很好地保存到本地存储,但由于此错误或缺少示例,我不知道如何使用此存储数据重新水化存储:/

似乎有两种使用装饰器的方法,但两者有很大的不同。我喜欢我从 vie 管理员那里找到的方法,因为这些组件非常干净,但我无法注入模块,因为 https://www.npmjs.com/package/vuex-persist#detailed 状态应该完成:/

我发现答案是示例 vue 管理应用程序的结构不正确。

而不是从模块中导出 class:

@Module({ name: 'authentication' })
export default class Authentication extends VuexModule implements IAuthenticationState {

然后将 class 作为模块注入索引并通过装饰器导出模块,同时将商店注入所述装饰器:

import Vue from 'vue';
import Vuex from 'vuex';
import Authentication from './modules/authentication';
import VuexPersistence from 'vuex-persist';
import { getModule } from 'vuex-module-decorators';

Vue.use(Vuex);

const vuexLocal = new VuexPersistence({
  storage: window.localStorage,
});

const store = new Vuex.Store({
  modules: {
    authentication: Authentication,
  },
  plugins: [vuexLocal.plugin],
});

export default store;
export const AuthenticationModule = getModule(Authentication, store);

结果是一切正常。