【发布时间】:2020-03-07 20:54:11
【问题描述】:
我正在开发一个基本上有两个流程的应用程序,即“FLOW1”和“FLOW2”。 每个流都有对应的路由。现在有一个我维护的开关,可以在流之间切换,相应的页面也应该被重定向。
例如:
FLOW1 有以下路线及其对应的组件:
F1R1:页面F1R1,
F1R2:页面F1R2,
F1R3 : 页面F1R3
FLOW2 有以下路线及其对应的组件:
F2R1:页面F2R1,
F2R2:页面F2R2,
F2R3 : 页面F2R3
这些路线是相互对应的,每当我更改配置文件时,对应于已更改流程的路线都应该更改。
所以我尝试过这样的事情,它有很多 has if's 。有没有更好的方法可以实现?
逻辑
navigate = (event) =>
{
let currentUrl = this.props.history.location.pathname;
let flow = event.target.name;
if(flow === "FLOW1")
{
if(currentUrl === "/F2R1")
this.props.history.push("/pageF1R1");
if(currentUrl === "/F2R2")
this.props.history.push("/pageF1R2");
if(currentUrl === "/F2R3")
this.props.history.push("/pageF1R3");
}
if(flow === "FLOW2")
{
if(currentUrl === "/F1R1")
this.props.history.push("/pageF2R1");
if(currentUrl === "/F1R2")
this.props.history.push("/pageF2R2");
if(currentUrl === "/F1R3")
this.props.history.push("/pageF2R3");
}
}
【问题讨论】:
-
你使用 React Router 进行路由吗?如果是这样,您可能想尝试使用它的组件来声明路由
-
@MartinHorváth 在这里,我试图以编程方式而不是声明。帮助将不胜感激。想要在问题中提到的开关更改上有所作为
标签: javascript reactjs ecmascript-6 react-router ecmascript-5