Reactjs 反应路由器v4。是不是;确切的;是否禁止任何嵌套路由?
我注意到,如果我创建Reactjs 反应路由器v4。是不是;确切的;是否禁止任何嵌套路由?,reactjs,routing,react-router,react-router-v4,Reactjs,Routing,React Router,React Router V4,我注意到,如果我创建,我不能嵌套任何其他内部组件,因为该道具“精确”阻止任何匹配并停止渲染 我正在尝试构建一个简单的应用程序,其中包含个人资料和订单页面。每个页面都有自己的侧边栏和一些订单项列表。我在每个页面中使用嵌套路由,根据当前位置重新指定适当的订单列表。为了使这个应用程序完美,我需要在起始位置呈现配置文件页面(例如“”) 我阅读了所有文档,唯一的解决方案是在路由组件中使用“精确”道具。但这是一个太脆弱的解决方案,因为如果我想使用嵌套路由进行侧栏或订单列表定义 是否有其他方法可以生成一个路由
,我不能嵌套任何其他内部组件,因为该道具“精确”阻止任何匹配并停止渲染
我正在尝试构建一个简单的应用程序,其中包含个人资料和订单页面。每个页面都有自己的侧边栏和一些订单项列表。我在每个页面中使用嵌套路由,根据当前位置重新指定适当的订单列表。为了使这个应用程序完美,我需要在起始位置呈现配置文件页面(例如“”)
我阅读了所有文档,唯一的解决方案是在路由组件中使用“精确”道具。但这是一个太脆弱的解决方案,因为如果我想使用嵌套路由进行侧栏或订单列表定义
是否有其他方法可以生成一个路由,该路由可以在“”位置上显示配置文件页,但也允许我使用嵌套路由
我当前的实施是下一步:
<Switch>
<Route path={'/'} exact component={Profile} />
<Route path={'/orders'} component={Orders} />
<Route component={NotFound} />
</Switch>
class Profile extends Component {
render() {
return (
<div className='profile_page'>
<Profile_Bar />
<div className='content'>
<Switch>
<Route path={'/subscribers'} component={User_List} />
<Route path={'/dashboard'} component={Dashboard} />
</Switch>
</div>
</div>
)
}
}
class Orders extends Component {
render() {
return (
<div className='orders_page'>
<Orders_Bar />
<div className='content'>
<Switch>
<Route path={'/active'} component={Orders_List} />
<Route path={'/completed'} component={Orders_List} />
</Switch>
</div>
</div>
)
}
}
const NotFound = ({ location }) => (
<div>
NotFound {location.pathname}
</div>
)
类概要文件扩展了组件{
render(){
返回(
)
}
}
类命令扩展组件{
render(){
返回(
)
}
}
const NotFound=({location})=>(
找不到{location.pathname}
)
在我以前的认识中,我使用
:
<Switch>
<Redirect from='/' to='/profile'/>
<Route path={'/profile'} component={Profile} />
<Route path={'/orders'} component={Orders} />
<Route component={NotFound} />
</Switch>
理想情况下,您的配置文件组件将按其自己的路线处理,如“/Profile”,并为您的“/”路线创建一个单独的组件,如主页:
<Switch>
<Route path={'/'} exact component={Home} />
<Route path={'/profile'} component={Profile} />
<Route path={'/orders'} component={Orders} />
<Route component={NotFound} />
</Switch>
…然后您的配置文件组件将具有如下子路由:
<Switch>
<Route path={'/profile/subscribers'} component={User_List} />
<Route path={'/profile/dashboard'} component={Dashboard} />
</Switch>
如果您确实不想在路由路径中使用“profile”,则可以将“/subscribers”和“/dashboard”路由添加到主路由中,这两个路由都呈现profile组件,但您可能仍然希望使用其自己的组件处理“/”路由:
<Switch>
<Route path={'/'} exact component={Home} />
<Route path={'/subscribers'} component={Profile} />
<Route path={'/dashboard'} component={Profile} />
<Route path={'/orders'} component={Orders} />
<Route component={NotFound} />
</Switch>
我想另一个选择是更改路由的顺序,以便“/orders”在“/”之前匹配。然后,您可以从“/”路由中删除精确的,以便子路由也匹配 但是,在这种情况下,您必须在配置文件组件中处理未找到路由,这并不理想
<Switch>
<Route path={'/orders'} component={Orders} />
<Route path={'/'} component={Profile} />
</Switch>
能否显示路线的全部代码?还有,你说的“嵌套路线”是什么意思?谢谢你。我已经做了,谢谢你的回答。这是一个很好的解决办法。我试试看