【发布时间】:2019-02-06 18:28:04
【问题描述】:
我创建了简单的 MobX 商店:
import { observable, action, computed } from "mobx"
interface UserInterface {
login: string
email: string
password: string
}
class User {
@observable users: UserInterface[] = []
@action addUser = (newUser: any) => {
this.users.push(newUser)
}
@action logMe = () => {
console.log("MobX works!");
}
@computed get userCount() {
return this.users.length
}
}
export const UserStore = new User()
这是使用来自此存储的数据的组件的开始:
import withStyles, { WithStyles } from "@material-ui/core/styles/withStyles"
import { inject, observer } from "mobx-react"
import { UserStore } from "../../../stores/user-store"
interface RegisterFormProps extends WithStyles<typeof RegisterFormStyles> {
userStore?: UserStore
}
@inject("userStore")
@observer
class LoginForm extends React.Component<RegisterFormProps> {
我在userStore?: UserStore 部分有一个 tslint 错误。它抛出错误:Type error: Cannot find name 'UserStore'. TS2304。这个类的路径是正确的。如果我将类型从 UserStore 更改为 any 它可以工作,但我想避免使用 any 类型。
@编辑。解决方案是在 MobX 存储文件的末尾包含以下行:
const UserStore = new User();
export { UserStore }
【问题讨论】:
标签: reactjs typescript mobx mobx-react