属性 在类型 'typeof CommonStore' 上不存在。 mobx-反应
Property does not exist on type 'typeof CommonStore'. mobx-react
我是 TypeScript 的新手,我尝试从另一个 class 调用一个动作,但我得到了这样的错误,也许无法通过导入使用函数,只能通过 @inject
?可能是什么问题我不明白
P.S。成立 @types
:
"@types/react": "^16.8.22",
"@types/react-dom": "^16.8.4",
"@types/react-router-dom": "^4.3.4",
import {
observable,
action,
autorun,
set,
toJS,
extendObservable
} from "mobx";
import commonStore from "./common";
export default class Authentication {
@action login = async() => {
this.inProgress = true;
this.errors = undefined;
try {
const token = await requestApi({
method: "post",
url: `/auth/login`,
data: {
login: this.username,
pass: this.password
}
});
commonStore.setToken(token); // Property 'setToken' does not exist on type 'typeof CommonStore'
} catch (error) {
axiosErrorHandler(error);
}
}
}
CommonStore
export default class CommonStore {
@observable token = window.localStorage.getItem("jwt");
constructor() {
reaction(
() => this.token,
token => {
if (token) {
localStorage.setItem("jwt", token);
} else {
localStorage.removeItem("jwt");
}
}
);
}
@action setToken(token: string) {
this.token = token;
}
}
index.ts商店
import Authentication from "./models/authentication";
import Common from "./models/common";
class ObservableListStore {
@observable authentication = new Authentication();
@observable common = new Common();
}
export const store = new ObservableListStore();
您导出了 class (CommonStore
),但您正在尝试将其用作已创建的对象。
您需要先创建实例,然后才能使用。
import commonStore from "./common";
const commonStoreInstance = new commonStore();
commonStoreInstance.setToken('token');
但您可能希望在 import
commonStore
的任何地方使用相同的实例。如果是这样,您需要在模块内创建实例并将其导出。
像这样:
class CommonStore {
@observable token = window.localStorage.getItem("jwt");
constructor() {
reaction(
() => this.token,
token => {
if (token) {
localStorage.setItem("jwt", token);
} else {
localStorage.removeItem("jwt");
}
}
);
}
@action setToken(token: string) {
this.token = token;
}
}
export const commonStore = new CommonStore();
然后
import commonStore from "./common";
commonStore.setToken('token');
我是 TypeScript 的新手,我尝试从另一个 class 调用一个动作,但我得到了这样的错误,也许无法通过导入使用函数,只能通过 @inject
?可能是什么问题我不明白
P.S。成立 @types
:
"@types/react": "^16.8.22",
"@types/react-dom": "^16.8.4",
"@types/react-router-dom": "^4.3.4",
import {
observable,
action,
autorun,
set,
toJS,
extendObservable
} from "mobx";
import commonStore from "./common";
export default class Authentication {
@action login = async() => {
this.inProgress = true;
this.errors = undefined;
try {
const token = await requestApi({
method: "post",
url: `/auth/login`,
data: {
login: this.username,
pass: this.password
}
});
commonStore.setToken(token); // Property 'setToken' does not exist on type 'typeof CommonStore'
} catch (error) {
axiosErrorHandler(error);
}
}
}
CommonStore
export default class CommonStore {
@observable token = window.localStorage.getItem("jwt");
constructor() {
reaction(
() => this.token,
token => {
if (token) {
localStorage.setItem("jwt", token);
} else {
localStorage.removeItem("jwt");
}
}
);
}
@action setToken(token: string) {
this.token = token;
}
}
index.ts商店
import Authentication from "./models/authentication";
import Common from "./models/common";
class ObservableListStore {
@observable authentication = new Authentication();
@observable common = new Common();
}
export const store = new ObservableListStore();
您导出了 class (CommonStore
),但您正在尝试将其用作已创建的对象。
您需要先创建实例,然后才能使用。
import commonStore from "./common";
const commonStoreInstance = new commonStore();
commonStoreInstance.setToken('token');
但您可能希望在 import
commonStore
的任何地方使用相同的实例。如果是这样,您需要在模块内创建实例并将其导出。
像这样:
class CommonStore {
@observable token = window.localStorage.getItem("jwt");
constructor() {
reaction(
() => this.token,
token => {
if (token) {
localStorage.setItem("jwt", token);
} else {
localStorage.removeItem("jwt");
}
}
);
}
@action setToken(token: string) {
this.token = token;
}
}
export const commonStore = new CommonStore();
然后
import commonStore from "./common";
commonStore.setToken('token');