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

Joh*_*ohn 4 decorator vuex vuex-modules

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

vuex.esm.js?2f62:261 Uncaught TypeError: 无法读取 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;
Run Code Online (Sandbox Code Playgroud)

这是我认为正在抛出错误的身份验证模块:

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);
Run Code Online (Sandbox Code Playgroud)

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

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

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

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

似乎有两种使用装饰器的方法,但两者都大不相同。我喜欢我从 vie admin 找到的方法,因为组件很好很干净,但我不能注入模块,因为https://www.npmjs.com/package/vuex-persist#detailed states 应该完成:/

Joh*_*ohn 9

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

而是从模块中导出类:

@Module({ name: 'authentication' })
export default class Authentication extends VuexModule implements IAuthenticationState {
Run Code Online (Sandbox Code Playgroud)

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

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);

Run Code Online (Sandbox Code Playgroud)

结果是一切正常。