【问题标题】:How can I start react app with specific page?如何启动具有特定页面的反应应用程序?
【发布时间】:2019-01-16 17:05:58
【问题描述】:

我有一个类似的路由器;

  <React.Fragment>
    <Switch>
      <Route exact path="/page1" component={Page1} />
      <PrivateRoute path="/page2" component={Page2} />
      <PrivateRoute path="/page3" component={Page3} />
      <Route path="*" component={NotFound} />
    </Switch>
  </React.Fragment>

但是当我启动我的应用程序时,它没有打开 Page1。它以空白页的形式打开。我怎样才能提供这个?

谢谢,

【问题讨论】:

    标签: reactjs react-router react-router-dom


    【解决方案1】:

    在顶部(或任何地方)添加“根”路由

      <React.Fragment>
        <Switch>
          <Route exact path="/" component={Page1} >
            <Redirect to="/page1" />
          </Route>
          <Route exact path="/page1" component={Page1} />
          <PrivateRoute path="/page2" component={Page2} />
          <PrivateRoute path="/page3" component={Page3} />
          <Route path="*" component={NotFound} />
        </Switch>
      </React.Fragment>
    

    【讨论】:

      【解决方案2】:

      您可以使用从/ 到/page1 的Redirect 组件:

      <Switch>
        <Redirect from="/" to="/page1" />
      
        <Route exact path="/page1" component={Page1} />
        <PrivateRoute path="/page2" component={Page2} />
        <PrivateRoute path="/page3" component={Page3} />
        <Route path="*" component={NotFound} />
      </Switch>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多