我有一个 react 应用程序,它充当仪表板并显示不同的链接
react 应用程序。用户可以通过单击按钮来选择应用程序。
简而言之,我需要根据用户选择重定向到不同的 URL。
在下面的示例代码中,尝试使用 withRouter 重定向到 URL。然而它给出了以下错误: 类型错误:无法读取未定义的属性“push”
我正在使用 React 15.6.1。
index.js
render(
<BrowserRouter>
<Home />
</BrowserRouter>, document.getElementById('app')
);
home.js
class Home extends React.Component {
constructor(props) {
super(props);
this.loadApp1 = this.loadApp1.bind(this);
}
loadApp1() {
this.props.route.push('/app1');
}
render() {
return (
<div>
<button onClick={this.loadApp1}>App1</button>
</div>
);
}
}
export default withRouter(Home);
最佳答案
您可以使用链接来执行此操作:
import { Link } from react-router-dom;
那么,
<button><Link to="/app1">App1</Link></button>
将路由到 App1 路由。
对于外部网站,您可以这样做:
loadApp1() {
window.location = "example.com";
}
关于reactjs - 如何在 React 中重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46679318/