【问题标题】:React router, route all except Path反应路由器,路由除 Path 之外的所有路由
【发布时间】:2021-06-25 13:32:13
【问题描述】:

我有一个反应路由器开关,我需要除“/terms”之外的任何路径来呈现 CompetitionContent 组件。以及呈现术语组件的 /terms 路径。

问题在于 url '/test' 正确呈现了 CompetitionContent 组件。但是当我导航到“/terms”时,会显示两个组件,而不仅仅是术语组件。

为什么会发生这种情况?我的理解是开关会在第一场比赛后退出?

function Competition(){

    return(
        <div class="container-fluid competition-container">
            <div class="col-12"><Banner/></div>
            <div class="col-12 competition-content">
            
        <Switch>
            <Route exact path="/terms" component={Terms}/>
            <Route path="/" component={CompetitionContent}/>
        </Switch>
       
            </div>
            <div class="col-12"><Footer/></div>
        </div>
    )
}
export default Competition

【问题讨论】:

  • 在所有路由中添加exact

标签: reactjs react-router


【解决方案1】:

您需要在&lt;Route exact path="/" component={CompetitionContent}/&gt;中添加exact

之后,您需要添加更多路由以将所有其他路由重定向到/

<Route path="*" component={() => <Redirect to="/" />}/>

【讨论】:

    猜你喜欢
    • 2018-06-02
    • 1970-01-01
    • 2018-11-05
    • 1970-01-01
    • 2018-08-13
    • 1970-01-01
    • 2019-02-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多