一些索引代码。tsx:
ReactDOM.render(
<MemoryRouter>
<LocaleProvider locale={zhCn}>
<App/>
</LocaleProvider>
</MemoryRouter>,
document.getElementById('root') as HTMLElement
);
应用程序的一些代码。tsx
interface AppProps{
history?: any
}
....
public route() {
if (!session.isLogin) {
this.props.history.push('/');
} else {
this.props.history.push('/root')
}
}
...
public render() {
return (
<Switch>
<Route exact path='/' component={Login} />
<Route exact path='/root' component={Root} />
</Switch>
)
}
当我写这个的时候,我会报错。无法读取未定义的属性“推送”,然后我修改了使用“带路由器”来修改它。
export default withRouter(App)
还报告了错误:错误TS2345:类型为“typeof App”的参数不能分配给类型为“ComponentType”的参数
类型“typeof App”不可分配给类型“无状态组件”
Type'typeof App'没有提供匹配的签名(道具:RouteComponentProps
有什么问题?
谢谢你
您在应用程序中的
需要从道具
。tsxreact路由器
例如:
interface AppProps extends RouteComponentProps {
// rest of props
}
然后,您应该能够使用:
this.props.history.push("/your-route");