请参阅我正在尝试设置一个React-Router应用程序。我有/parties
,所有参与者的列表,然后有/parties/:id
,这给了我一个聚会的详细页面,还有/parties/create
,其中有创建聚会的表格。
现在,我正在尝试将主资源始终放在屏幕左侧,而将子资源始终放在屏幕右侧。
这是我的设置方式:
return <div>
<div>
<nav>
<h1>Gamevote</h1>
<AuthWidget authService={this.authService}/>
</nav>
<div class="content cnt" ref={this.content}>
<this.PrivateRoute path="/" exact component={() => <Redirect to="/parties"/>} />
<Route path="/register" exact component={() => <Register/>} />
<Route path="/login" exact component={() => <Login authService={this.authService}/>} />
<this.PrivateRoute path="/parties/:selectedParty?" component={this.AutowiredPartyList} />
</div>
<div class="big-content cnt" ref={this.bigContent}>
<this.PrivateRoute path="/party/create" partyService={this.partyService} exact component={() => <this.AutowiredCreateParty/>}/>
<this.PrivateRoute path="/parties/:party" exact component={PartyView}/>
</div>
<this.PrivateRoute path="/logout" exact component={this.Logout}/>
</div>
</div>
}
但是现在当我转到/parties/create
时,会显示一个创建聚会表单和一个带有id party的聚会详细信息。
是否有方法将/parties/create
从比赛/parties/:id
中排除?
答案 0 :(得分:0)
如果您想同时拥有/parties/create
和/parties/:id
,请更改顺序:
path =“ / parties / create”(第一)
path =“ / parties /:id”(最后)
如果另一个路径带有/ parties /:something?在进入此路线之前已经存在。
那是因为"/parties/:id"
与/parties/#####
之后的任何内容匹配
还要注意,您将必须添加Switch
组件,以便一次仅呈现一个路径。
import React from "react";
import ReactDOM from "react-dom";
import { BrowserRouter as Router, Route, Link, Switch } from "react-router-dom";
function Index() {
return <h2>Home</h2>;
}
function About() {
return <h2>About create</h2>;
}
function AboutId() {
return <h2>About with ID</h2>;
}
function Users() {
return <h2>Users</h2>;
}
function AppRouter() {
return (
<Router>
<div>
<nav>
<ul>
<li>
<Link to="/">Home</Link>
</li>
<li>
<Link to="/users/">Users</Link>
</li>
<li>
<Link to="/about/create">About</Link>
</li>
<li>
<Link to="/about/1">About with id</Link>
</li>
</ul>
</nav>
<Switch>
<Route path="/" exact component={Index} />
<Route path="/users/" component={Users} />
<Route path="/about/create" component={About} />
<Route path="/about/:id" component={AboutId} />
</Switch>
</div>
</Router>
);
}
const rootElement = document.getElementById("root");
ReactDOM.render(<AppRouter />, rootElement);