如何在另一个布局中限制React Router v6的路由?

huangapple go评论72阅读模式
英文:

How to restrict routes in another layout react router v6

问题

我有三种布局:AdminAppCompanySetup。我需要在Admin布局中限制App布局的路由,反之亦然。我在下面分享了我的代码。

Routes.js

<Route path='/'>
  <Route path='app'>
    <Route index element={<Home />}></Route>
    <Route
      path='search/category/:category'
      element={<SearchPage />}
    ></Route>
    <Route
      path='search/category/:category/name/:name'
      element={<SearchPage />}
    ></Route>
  </Route>
  <Route path='admin'>
    <Route element={<AdminRouteProvider></AdminRouteProvider>}>
      <Route
        path='dashboard'
        element={<AdminDashboard />}
      ></Route>
    </Route>
  </Route>
</Route>

AppLayout.js

<Layout className='min-vh-100 bg-white'>
  <Content className='p-4'>
    <Outlet />
  </Content>
</Layout>

AdminLayout.js

<Layout className='min-vh-100 bg-white'>
  <Sider width={240} className='bg-white border-end'>
    <Sidebar />
  </Sider>
  <Layout>
    <Header className='bg-body border-bottom'>
      <HeaderNav></HeaderNav>
    </Header>
    <Content className='p-5 bg-body'>
      <AdminRouteProvider>
        <Outlet></Outlet>
      </AdminRouteProvider>
    </Content>
  </Layout>
</Layout>

这里我为'/app''/admin'有不同的布局。我的问题是在Admin布局中允许'/app'。如何限制这一点?

英文:

I am having three layouts and they are Admin, App, CompanySetup. I need to restrict routes of app layout in admin layout and vice-versa. I shared my Code below.

Routes.js

&lt;Route path=&#39;/&#39;&gt;
  &lt;Route path=&#39;app&#39;&gt;
    &lt;Route index element={&lt;Home /&gt;}&gt;&lt;/Route&gt;
    &lt;Route
      path=&#39;search/category/:category&#39;
      element={&lt;SearchPage /&gt;}
    &gt;&lt;/Route&gt;
    &lt;Route
      path=&#39;search/category/:category/name/:name&#39;
      element={&lt;SearchPage /&gt;}
    &gt;&lt;/Route&gt;
  &lt;/Route&gt;
  &lt;Route path=&#39;admin&#39;&gt;
    &lt;Route element={&lt;AdminRouteProvider&gt;&lt;/AdminRouteProvider&gt;}&gt;
      &lt;Route
        path=&#39;dashboard&#39;
        element={&lt;AdminDashboard /&gt;}
      &gt;&lt;/Route&gt;
    &lt;/Route&gt;
  &lt;/Route&gt;
&lt;/Route&gt;

AppLayout.js

&lt;Layout className=&#39;min-vh-100 bg-white&#39;&gt;
  &lt;Content className=&#39;p-4&#39;&gt;
    &lt;Outlet /&gt;
  &lt;/Content&gt;
&lt;/Layout&gt;

AdminLayout.js

&lt;Layout className=&#39;min-vh-100 bg-white&#39;&gt;
  &lt;Sider width={240} className=&#39;bg-white border-end&#39;&gt;
    &lt;Sidebar /&gt;
  &lt;/Sider&gt;
  &lt;Layout&gt;
    &lt;Header className=&#39;bg-body border-bottom&#39;&gt;
      &lt;HeaderNav&gt;&lt;/HeaderNav&gt;
    &lt;/Header&gt;
    &lt;Content className=&#39;p-5 bg-body&#39;&gt;
      &lt;AdminRouteProvider&gt;
        &lt;Outlet&gt;&lt;/Outlet&gt;
      &lt;/AdminRouteProvider&gt;
    &lt;/Content&gt;
  &lt;/Layout&gt;
&lt;/Layout&gt;

Here I am having separate layouts for &#39;/app&#39; and &#39;/admin&#39;. My issue is in admin layout &#39;/app&#39; is allowed. How to restrict that?

答案1

得分: 0

你可以使用useEffectuseLayoutEffect来防止访问'/app'路径。

例如:

useEffect(() => {
  if (!JSON.parse(localStorage.getItem('admin'))) {
    window.location.href = "/app";
  }
}, []);

这只是一个示例,根据你的需求,代码可能会有所变化。如果你仍然遇到问题,请告诉我。

英文:

you can use useEffect/useEffectLayout to prevent from access the route or path of '/app'.

for example-

useEffect(()=&gt;{
if(!JSON.parse(localStorage.getItem(&#39;admin&#39;)){
window.location.href=&quot;/app&quot;
}
},[])

this is just an example may this code also change according to your need. If you still facing issue just lemme know.

答案2

得分: 0

从我可以看出,您没有为“/app”或“/admin”渲染任何实际的布局路由。导入AppLayoutAdminLayout,然后渲染适当的布局路由。

英文:

From what I can tell you are not rendering any actual layout routes for either &quot;/app&quot; or &quot;/admin&quot;. Import AppLayout and AdminLayout1 and render the appropriate layout routes.

import AppLayout from &#39;../path/to/AppLayout&#39;;
import AdminLayout from &#39;../path/to/AdminLayout&#39;;

...

&lt;Route path=&quot;/&quot;&gt;
  &lt;Route path=&quot;app&quot; element={&lt;AppLayout /&gt;}&gt;
    &lt;Route index element={&lt;Home /&gt;} /&gt;
    &lt;Route path=&quot;search/category/:category&quot;&gt;
      &lt;Route index element={&lt;SearchPage /&gt;} /&gt;
      &lt;Route path=&quot;name/:name&quot; element={&lt;SearchPage /&gt;} /&gt;
    &lt;/Route&gt;
  &lt;/Route&gt;
  &lt;Route path=&quot;admin&quot; element={&lt;AdminLayout /&gt;}&gt;
    &lt;Route path=&quot;dashboard&quot; element={&lt;AdminDashboard /&gt;} /&gt;
    &lt;Route path=&quot;*&quot; element={&lt;Navigate to=&quot;dashboard&quot; replace /&gt;} /&gt;
  &lt;/Route&gt;
&lt;/Route&gt;

huangapple
  • 本文由 发表于 2023年2月19日 00:23:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/75494704.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定