【问题标题】:How to use react-router to navigate component with multiple pages如何使用 react-router 导航具有多个页面的组件
【发布时间】:2019-11-17 16:11:16
【问题描述】:

假设我有 2 个页面和几个组件。第一个页面是登录页面,另一个页面是主菜单。登录页面只有 1 个组件。用户登录网站后,我希望 react-router 导航到主菜单,主菜单在下方有导航栏和一些组件。我希望它能够导航到每个组件并始终将导航栏保持在顶部,即使 URL 已更改。

这是我尝试过的代码

// Inside the root component
<BrowserRouter>
     <Route path="/menu" component={MenuForm}/>
     <Route path="/login" component={LoginForm}/>
</BrowserRouter>

//Inside the menu page component  
<Navbar/>
<Route path="/shop" component={Shop}/>
<Route path="/categories" component={Categories}/>

使用此代码,我只能导航到菜单页面和登录页面,但无法导航到作为主菜单子组件的商店和类别

【问题讨论】:

  • 所以导航栏应该在菜单和登录路径上都可用是吗?
  • 没有导航栏应该只出现在主菜单上

标签: javascript reactjs react-router-dom


【解决方案1】:

你需要有主页

<Route path="/home" component={Home}/> // all your menu and everything here

现在,如果您想在家中访问菜单 你可以这样做

<Route path="/home/menu" component={Menu}/>

该组件将在您设置嵌套路由的主页中呈现,以便匹配的路由组件将呈现

<Route path="/home/menu" component={menu}/>
<Route path="/home/profile" component={Profile}/>

【讨论】:

  • 我试过你的代码,它工作正常,但它可以像 path="/menu" 或 path="/profile" 独立。
  • 是的,如果你想在根级别显示唯一的菜单,那么这很好
【解决方案2】:

我想您应该考虑创建容器组件。第一个容器将包含您的登录路由,即“Auth Container”,而另一个组件应位于 App 路由中,即“App Container”。您可以拥有自己的包装器。

const AppRoutes = () => {
    return (
        <>
            <Navigation scrolling={scrolling} />
            <Switch>
                <ProtectedRoute exact path="/profile" component={UserProfile} />
                <ProtectedRoute exact path="/my-orders" component={MyOrders} />
                <ProtectedRoute path="/my-saved-result" component={SavedResults} />
            </Switch>
        </>
    )
}

【讨论】:

    【解决方案3】:

    如果您使用react-router v4,您可以Switch 组件以声明方式定义您的路线,如下所示:

    import { Switch, Route, Redirect } from 'react-router-dom';
    
    const MenuForm = () => (
      <div className="app-routes">
        <NavBar />
        <Switch>
        <Route exact path="/menu">
          <Redirect to="/menu/shop" />
          </Route>
          <Route path="/menu/shop" component={Shop} />
          <Route path="/menu/categories" component={Categories} />
        </Switch>
      </div>
    );
    

    【讨论】:

    • 如果你有/login & /password-reset 没有&lt;NavBar&gt; 的路由你能告诉我怎么做吗?
    【解决方案4】:

    我是这样用的

        <div className="main-panel">
            <Navbar />
            <div className="content" style={{ backgroundColor: '#f4f3ef' }}>
                <Switch>
                    <Route path="/" exact component={Home} />
                    <Route path="/reports" component={ReportMain} />
                    <Route path="/showWebService" component={ShowWebService} />
                </Switch>
            </div>         
        </div>
    

    当您导航到链接时,导航栏是静态的,它只获取这些组件渲染。

    <Link to="/reports">
      <p>Reports</p>
    </Link>
    

    通过链接调用组件

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-15
      • 2020-07-09
      • 2016-01-08
      • 2016-09-28
      • 1970-01-01
      • 2023-02-20
      • 2022-12-17
      • 2020-07-31
      相关资源
      最近更新 更多