【问题标题】:Redirecting path in ReactJS's react-router-dom's routeReactJS react-router-dom 路由中的重定向路径
【发布时间】:2021-07-01 17:14:41
【问题描述】:

我使用数组为同一个组件声明了两个不同的路径。

        <Router>    
          <HeaderBN/>
            <Switch>
                <Route exact path="/" component={Home}/>
                <Route path={["/about", "/aboutus"]} component={About}/>
            </Switch>
        </Router>

但现在我想如果有人访问“/about”路径,那么他会自动重定向到“/aboutus”路径。我怎样才能做到这一点?我阅读了 react-router-dom 文档,但没有找到解决此问题的任何方法。

【问题讨论】:

    标签: reactjs react-router react-hooks


    【解决方案1】:

    我建议你从数组中取出about 并创建一个包含重定向逻辑的新路由。

     <Route exact path="/about">
         <Redirect to="/aboutus" />
      </Route>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-30
      • 1970-01-01
      • 2018-01-28
      • 2019-02-05
      • 1970-01-01
      相关资源
      最近更新 更多