[英]Vuex Store is in Typescript Class undefined
I have the problem, that I want to refer a Typescript class to my Vuex
Store. 我有一个问题,我想将Typescript类引用到我的
Vuex
商店。 But the store is always undefined. 但是商店总是不确定的。
So this is my store 这是我的商店
import Vue from 'vue';
import Vuex from 'vuex';
import { SessionManager } from '@/classes/worker';
import { User } from '@/classes/user';
Vue.use(Vuex);
export default new Vuex.Store({
state: {
user : new User(),
store : new SessionManager(),
},
mutations: {
},
actions: {
},
getters:{
getUser : state => {
return state.user;
}
},
});
And in my SessionManager
class I want to do something like this in the constructor 在我的
SessionManager
类中,我想在构造函数中做类似的事情
import store from '@/store';
export class SessionManager {
public constructor() {
let user = store.state.user as User;
console.log(store);
}
}
I have the Vue-Extensions for Chrome. 我有适用于Chrome的Vue扩展程序。 So when I set a Break point in Chrome in the line before I use the store, and watch the state of the store in the extension he already exist.
因此,当我在使用商店之前在该行的Chrome中设置断点时,并在扩展中观察商店的状态,他已经存在。
Can someone help me? 有人能帮我吗?
It looks like you have a cyclic dependency: your store file is calling new SessionManager()
before it calls new Vuex.Store
, so when the SessionManager
constructor runs, the store has not yet been created and exported. 似乎您具有循环依赖性:您的存储文件在调用
new Vuex.Store
之前先调用new SessionManager()
,因此当SessionManager
构造函数运行时,尚未创建和导出存储。 I guess you should avoid accessing the store from the SessionManager
constructor. 我想您应该避免从
SessionManager
构造函数访问存储。 I don't know Vuex, so there may be another way this is supposed to be done. 我不了解Vuex,所以可能还有另一种方法可以做到。
To understand what is happening, let's look at a simplified version of the code: 要了解发生了什么,让我们看一下代码的简化版本:
let store;
class SessionManager {
constructor() {
console.log(store);
}
}
let sm = new SessionManager();
store = new Vuex.Store({state: {store: sm, ...}, ...});
At the time new SessionManager()
runs, store
has not been initialized yet, so it is undefined
. 在运行
new SessionManager()
, store
尚未初始化,因此undefined
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.