首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >vuex不加载用vuex模块装饰的模块

vuex不加载用vuex模块装饰的模块
EN

Stack Overflow用户
提问于 2019-04-25 06:27:40
回答 1查看 7.4K关注 0票数 4

当试图将带有vuex模块装饰符的存储模块加载到初始化器中时,会出现此错误:

vuex.esm.js?2f62:261 Uncaught TypeError:无法读取属性‘getter’的属性‘getter’,在assertRawModule () at Array.forEach () at assertRawModule (vuex.esm.js?2f62:260) at ModuleCollection.register (vuex.esm.js?2f62:186) at assertRawModule (vuex.esm.js?2f62:200) at Array.forEach (vuex.esm.js?2f62:75) at Array.forEach() at forEachValue (vuex.esm.js?2f62:75) at ModuleCollection。在新ModuleCollection (vuex.esm.js?2f62:160)注册(vuex.esm.js?2f62:160)

index.ts文件非常简单,在我将模块介绍给初始化程序之前,所有文件都可以工作:

代码语言:javascript
复制
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;

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

代码语言:javascript
复制
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获得了这个设置

我不知道这是否是一个bug,或这是否是一个设置问题,但完全停留在这里,因为我打算使用vuex持久化在这里“补水”存储后,页面重新加载。

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

目前,我已经很好地将所有状态保存到本地存储,但由于这个错误或缺乏经验,我不知道如何用存储的数据重新补充存储:/

似乎有两种方式使用装饰,但两者是完全不同的。我喜欢从vie管理中找到的方法,因为组件很好,很干净,但是我不能像https://www.npmjs.com/package/vuex-persist#detailed状态应该做的那样注入模块:/

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-04-25 07:26:30

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

相反,要从模块导出类:

代码语言:javascript
复制
@Module({ name: 'authentication' })
export default class Authentication extends VuexModule implements IAuthenticationState {

然后将类作为模块注入索引,并通过装饰器导出模块,但也将存储插入到所述装饰器中:

代码语言:javascript
复制
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);

结果就是这一切都奏效了。

票数 10
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55843052

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档