当尝试将带有 vuex-module-decorators 的存储模块加载到初始化程序中时,我收到此错误:
vuex.esm.js?2f62:261 未捕获类型错误:无法读取属性
Array.forEach 的 eval (vuex.esm.js?2f62:261) 处未定义的“getters”
() 在assertRawModule (vuex.esm.js?2f62:260) 处
ModuleCollection.register (vuex.esm.js?2f62:186) 在 eval
(vuex.esm.js?2f62:200) 在 eval (vuex.esm.js?2f62:75) 在 Array.forEach
() 在 forEachValue (vuex.esm.js?2f62:75) 在 ModuleCollection.register
(vuex.esm.js?2f62:199) 在新的 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 https://github.com/calvin008/vue3-admin
我不知道这是一个错误还是一个设置问题,但完全卡在这里,因为我打算在页面重新加载后使用 vuex-persist 来“补充”商店。
使用此库声明存储的另一种完全不同的方式如下:https://github.com/eladcandroid/typescript-vuex-example/blob/master/src/components/Profile.vue https://github.com/eladcandroid/typescript-vuex-example/blob/master/src/components/Profile.vue但是当在 vue3-admin 中它的语法似乎会变得非常冗长,它完全位于与组件相反的存储中。
目前,我已将所有状态很好地保存到本地存储中,但由于此错误或缺乏示例,我不知道如何使用此存储的数据补充存储:/
似乎有两种使用装饰器的方法,但两者都有很大不同。我喜欢我从 vie 管理员那里找到的方法,因为组件漂亮干净,但我无法注入模块https://www.npmjs.com/package/vuex-persist#detailed https://www.npmjs.com/package/vuex-persist#detailed国家应该这样做:/