gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 00:26:53 31 4
gpt4 key购买 nike

尝试将带有 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 在页面重新加载后“重新水化”商店。

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

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

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

最佳答案

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

而是从模块中导出类:

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

然后将类作为模块注入(inject)索引并通过装饰器导出模块,同时将存储注入(inject)所述装饰器:
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);


结果是一切正常。

关于decorator - vuex 不加载用 vuex-module-decorators 装饰的模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55843052/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com