【发布时间】:2019-11-18 22:53:13
【问题描述】:
我正在使用 mongodb 创建注册登录模块,表达和反应,但是在创建这个时我遇到了属性历史不存在错误:
“只读”和“只读”类型上不存在属性“历史”'.ts(2339)
import React, { Component } from 'react'
import { login } from './userfunctions'
interface userprops{}
interface data{
email:string;
password:string;
errors: string;
}
//主类:
class Login extends Component<userprops, data> {
constructor(props: userprops) {
super(props)
this.state = {
email: '',
password:'',
errors: ''
}
this.onChange = this.onChange.bind(this) //binding the function
this.onSubmit = this.onSubmit.bind(this) //binding the function
}
onChange(e:any) { //on change function
this.setState({ email: e.target.value })
}
onSubmit(e:any) { //on submitting the form in render function this function will fire
e.preventDefault()
const user = {
email: this.state.email,
password: this.state.password
}
login(user).then(res => { //I had created login function in another component
if (res) {
this.props.history.push(`/profile`) //this line is giving error
}
})
}
render() {
//rendering data
}
export default Login
错误信息:
“只读”和“只读”类型上不存在属性“历史”'.ts(2339)
我已经尝试了很多东西,但这个错误不会消失
这应该运行
【问题讨论】:
-
你的
userprops界面中没有history。 -
非常感谢。通过添加界面 userprops{history:any} 解决了我的问题
标签: reactjs typescript react-native react-router