当我使用导航栏和按钮在页面之间导航时,浏览器上的 URL 会正常更改。但是一旦我尝试手动修改 URL,它就不起作用了!
示例 1
- 第 1 步:我打开localhost:3000
- 第 2 步:我点击 Athletes 转到localhost:3000/Athletes
- 第 3 步:我选择一个运动员去localhost:3000/Athletes/2/AthleteSessions/
- 第 4 步:我手动将 URL 更改为localhost:3000/Athletes然后我选择一个运动员,我被重定向到localhost:3000/Athletes/2/AthleteSessions/直到现在,但是当我修改 URL 以放置localhost:3000 /Athletes/用斜线然后我选择一个运动员它不起作用,我被重定向到localhost:3000/Athletes/Athletes/2/AthleteSessions/
示例 2
- 步骤 1、2 和 3 与第一个示例相同。
- 第 4 步:我选择一个 AthleteSession 转到localhost:3000/Athletes/2/AthleteSessions/1/TrainingSession/它工作正常,但是当我尝试通过手动修改 URL localhost:3000/Athletes/2返回上一页时/AthleteSessions然后我再次选择一个 AthleteSession,它不起作用,我被重定向到localhost:3000/Athletes/2/1/TrainingSession/
应用程序.js
import { Route, Switch, Redirect } from "react-router-dom";
<Route path="/Athletes" exact component={Athletes} />
<Route path="/Athletes/:id/AthleteSessions" exact component={AthleteSessions} />
<Route path="/Athletes/:athleteId/AthleteSessions/:athleteSessionId/TrainingSession"
exact component={AthleteTrainingSession} />
索引.js
import { BrowserRouter as Router } from "react-router-dom";
render( <Router> <App /> </Router>, document.getElementById("root") );
运动员.js
onClick={() => {
this.props.history.push({
pathname:
"Athletes/" + ath.AthleteId + "/AthleteSessions/",
});
}}
AthleteSessions.js
passedAthleteId = this.props.match.params.id;
onClick={() => {
this.props.history.push({
pathname:
athSess.AthleteSessionId + "/TrainingSession/",
});
}}