Javascript 无法读取属性';推动';在登录页面上未定义的
我正在用JWT做一个登录页面,并试图在使用这个.props.history.push(“/”)登录到主页后重定向该页面,但我得到以下类型错误:无法读取未定义的属性“push”。我不知道我做错了什么Javascript 无法读取属性';推动';在登录页面上未定义的,javascript,reactjs,authentication,error-handling,typeerror,Javascript,Reactjs,Authentication,Error Handling,Typeerror,我正在用JWT做一个登录页面,并试图在使用这个.props.history.push(“/”)登录到主页后重定向该页面,但我得到以下类型错误:无法读取未定义的属性“push”。我不知道我做错了什么 类登录扩展组件{ 建造师(道具){ 超级(道具); 此.state={ 电子邮件:“cihemzine@gmail.com", 密码:“测试”, }; } handleChange=(e)=>{ 这是我的国家({ [e.target.name]:e.target.value, }); }; 登录=(
类登录扩展组件{
建造师(道具){
超级(道具);
此.state={
电子邮件:“cihemzine@gmail.com",
密码:“测试”,
};
}
handleChange=(e)=>{
这是我的国家({
[e.target.name]:e.target.value,
});
};
登录=()=>{
const{email,password}=this.state;
axios(“/users/login”{
方法:“张贴”,
数据:{
电子邮件,
密码
},
})
。然后((响应)=>{
setItem('token',response.data.token);
console.log(response.data);
this.props.history.push(“/”);
})
.catch((错误)=>{
console.log(错误);
});
};
render(){
返回(
登录
)
}
}
导出默认登录代码>如果道具中没有类似“历史记录、匹配等”的内容,则需要检查是否使用登录组件作为路由
<Route path ='' component={Login} />
当你console.log(props.history)
你看到了什么?它说props没有定义。你可以检查props中没有类似“history,match等”的内容吗?我使用登录作为路径,就像这样:试着像
那样使用它。然后将其作为子级渲染,您可以使用子级作为渲染方法并手动传递历史等道具
import { useHistory } from "react-router-dom"
or
import { withRouter } from "react-router";