Reactjs 在带有MobX的Typescript中找不到名称
我创建了简单的MobX商店:Reactjs 在带有MobX的Typescript中找不到名称,reactjs,typescript,mobx,mobx-react,Reactjs,Typescript,Mobx,Mobx React,我创建了简单的MobX商店: import { observable, action, computed } from "mobx" interface UserInterface { login: string email: string password: string } class User { @observable users: UserInterface[] = [] @action addUser = (newUser: any) => {
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> {
export const UserStore=new User()
表示您正在导出变量UserStore
,变量不能用作类型(但类可以)。您必须导出类UserStore
在接口UserStore?中使用它:User
您尝试过这个吗
const UserStore = new User();
export { UserStore }
不起作用。我收到错误
this.props.userStore.addUser不是一个函数
如何正确导出它?好,但是否出现错误类型错误:找不到名称“userStore”。TS2304是否消失?您是否也可以显示定义和调用addUser
方法的部分代码?addUser()
方法在存储中声明,请看第一段。是的,您是对的。但是您确定在提供程序组件中也有类似的内容:
其中userStore=new User()
我建议导出类用户…
和从…
导入{class},而不是导出默认值。
const UserStore = new User();
export { UserStore }