如何为访客用户页面创建路由?

发布于 2025-02-07 05:43:16 字数 2164 浏览 0 评论 0原文

我有一个中央路由配置文件,该文件包含所有路由。到目前为止,路由配置包括我仪表板页面的路由。这就是它的样子:

const routes = [
  {
    type: "collapse",
    name: "Dashboards",
    key: "dashboards",
    icon: <Icon fontSize="small">dashboard</Icon>,
    noCollapse: "true",
  },
  {
    type: "collapse",
    name: "Tools",
    key: "apps",
    icon: <Icon>apps</Icon>,
    collapse: [
      {
        name: "First Page",
        key: "first",
        icon: <Icon>apps</Icon>,
        href: `first_page`,
      },
      {
        name: "Second Page",
        key: "second",
        route: "/",
        component: <SecondPage/>,
      },
      {
        name: "Third Page",
        key: "third",
        icon: <Icon>table</Icon>,
        href: `third_page`,
      },
    ],
  },
  {
    type: "collapse",
    name: "Settings",
    key: "settings",
    icon: <Icon>settings</Icon>,
    href: `settings`,
    noCollapse: true,
  },
  {
    type: "collapse",
    name: "Sign Out",
    key: "signout",
    icon: <Icon>logout</Icon>,
    href: `sign_out`,
    noCollapse: true,
  },
];

这就是我在app.js中渲染路由的方式:

      <Routes>
        {getRoutes(routes)}
        <Route path="*" element={<Navigate to="/" />} />
      </Routes>

这就是我在routes list上循环的方式:

  const getRoutes = (allRoutes) =>
    allRoutes.map((route) => {
      if (route.collapse) {
        return getRoutes(route.collapse);
      }
      if (route.route) {
        return <Route exact path={route.route} element={route.component} key={route.key} />;
      }
      return null;
    });

我想添加一个宾客用户表格的新路线可以由未经身份验证的用户访问,这意味着他们应该无法访问仪表板页面。但是我不确定它是否应嵌套在路由配置文件中,否则我应该在app.js中这样创建

      <Routes>
        {getRoutes(routes)}
        <Route
          exact
          path="/guest_user"
          element={<GuestUser/>}
          key={"guest"}
        />
        <Route path="*" element={<Navigate to="/" />} />
      </Routes>

它说明目的。

I have a central routes config file that holds all of my routes. So far, the routes config consists of routes for my dashboard pages. Here is what it looks like:

const routes = [
  {
    type: "collapse",
    name: "Dashboards",
    key: "dashboards",
    icon: <Icon fontSize="small">dashboard</Icon>,
    noCollapse: "true",
  },
  {
    type: "collapse",
    name: "Tools",
    key: "apps",
    icon: <Icon>apps</Icon>,
    collapse: [
      {
        name: "First Page",
        key: "first",
        icon: <Icon>apps</Icon>,
        href: `first_page`,
      },
      {
        name: "Second Page",
        key: "second",
        route: "/",
        component: <SecondPage/>,
      },
      {
        name: "Third Page",
        key: "third",
        icon: <Icon>table</Icon>,
        href: `third_page`,
      },
    ],
  },
  {
    type: "collapse",
    name: "Settings",
    key: "settings",
    icon: <Icon>settings</Icon>,
    href: `settings`,
    noCollapse: true,
  },
  {
    type: "collapse",
    name: "Sign Out",
    key: "signout",
    icon: <Icon>logout</Icon>,
    href: `sign_out`,
    noCollapse: true,
  },
];

This is how I am rendering the routes in App.js:

      <Routes>
        {getRoutes(routes)}
        <Route path="*" element={<Navigate to="/" />} />
      </Routes>

This is how I am looping over the routes list:

  const getRoutes = (allRoutes) =>
    allRoutes.map((route) => {
      if (route.collapse) {
        return getRoutes(route.collapse);
      }
      if (route.route) {
        return <Route exact path={route.route} element={route.component} key={route.key} />;
      }
      return null;
    });

I want to add a new route for a guest user form to the project that can be accessed by an unauthenticated user, meaning they shouldn't be able to access the dashboard pages. But I'm not sure whether it should be nested somewhere in the routes config file or I should create it like this in App.js:

      <Routes>
        {getRoutes(routes)}
        <Route
          exact
          path="/guest_user"
          element={<GuestUser/>}
          key={"guest"}
        />
        <Route path="*" element={<Navigate to="/" />} />
      </Routes>

Note: names have been amended for explanation purposes.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

最单纯的乌龟 2025-02-14 05:43:16

您可以创建一个 preectedRoute .js文件/文件夹,您可以在其中编写如下的逻辑。

import React, { useEffect } from "react";
import { useNavigate } from "react-router-dom";

function ProtectedRoute(props) {
  // eslint-disable-next-line react/prop-types
  const { Component } = props;
  const navigate = useNavigate();
  useEffect(() => {
    const token = localStorage.getItem("token");
    // eslint-disable-next-line no-console
    if (!token) {
      navigate("/");
    }
  });
  return <Component {...props} />;
}

export default ProtectedRoute;

之后,您可以在路线数组中进行以下更改这样。

{
    type: "collapse",
    name: "Profile",
    key: "profile",
    icon: <Icon fontSize="small">person</Icon>,
    route: "/profile",
    component: <ProtectedRoute Component={Profile} />,
  },

可以在preectedRoute.js中添加其他功能,例如基于角色等。

You can create a ProtectedRoute.js file/folder where you can write the logic as follows.

import React, { useEffect } from "react";
import { useNavigate } from "react-router-dom";

function ProtectedRoute(props) {
  // eslint-disable-next-line react/prop-types
  const { Component } = props;
  const navigate = useNavigate();
  useEffect(() => {
    const token = localStorage.getItem("token");
    // eslint-disable-next-line no-console
    if (!token) {
      navigate("/");
    }
  });
  return <Component {...props} />;
}

export default ProtectedRoute;

After that you can do the following changes in routes array just like this.

{
    type: "collapse",
    name: "Profile",
    key: "profile",
    icon: <Icon fontSize="small">person</Icon>,
    route: "/profile",
    component: <ProtectedRoute Component={Profile} />,
  },

Other functionalities could be added like role based etc. in the ProtectedRoute.js .

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文