以编程方式 React Router v4 导航
React Router v4 navigation programmatically
当我使用带有重定向的 React 路由器时,Link 或 NavLink 一切都很好。但是我需要一个函数,它可以以编程方式将我导航到一条路线。
这就是我想要的:
if(a == 1){
this.context.history.push("/")
}
else {
this.context.history.push("/home");
}
如您所见,上下文历史记录不再存在于 React Router v4 中。我无法在 js 代码中编写 withroute 进行导航。谁能告诉我如何在js代码中直接导航?
谢谢
路由器会在你的props中添加一个history
,你可以这样使用:
this.props.history.push('/mypath')
查看此以获取更多信息:
当我使用带有重定向的 React 路由器时,Link 或 NavLink 一切都很好。但是我需要一个函数,它可以以编程方式将我导航到一条路线。
这就是我想要的:
if(a == 1){
this.context.history.push("/")
}
else {
this.context.history.push("/home");
}
如您所见,上下文历史记录不再存在于 React Router v4 中。我无法在 js 代码中编写 withroute 进行导航。谁能告诉我如何在js代码中直接导航?
谢谢
路由器会在你的props中添加一个history
,你可以这样使用:
this.props.history.push('/mypath')
查看此以获取更多信息: