使用 react router v4 和 Typescript 以编程方式导航时出错:
'Readonly & Readonly<{ children?: ReactNode; 类型不存在属性“历史” }>
我想在 API 调用成功或失败时重定向到特定路径。但无法做到这一点。
路由器代码
import { BrowserRouter as Router , Switch , Route} from 'react-router-dom';
import * as React from 'react';
class App extends React.Component<{}, {}> {
render()
{
<Router>
<Switch>
<Route exact={true} path='/' component={Login}/>
<Route path='/home' component={Home}/>
<Route path='/test' component={Test}/>
</Switch>
</Router>
}
}
export default App;
零件
import { withRouter } from "react-router-dom";
import * as React from 'react';
class Test extends React.Component<IProps, IState> {
handleSubmit = async() => {
//code for API calls
this.props.history.push("/home") // error at this line
}
render() {
// Form with validations and a submit handler
}
}
export default withRouter(Test);