如何为访客用户页面创建路由?
我有一个中央路由配置文件,该文件包含所有路由。到目前为止,路由配置包括我仪表板页面的路由。这就是它的样子:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以创建一个 preectedRoute .js文件/文件夹,您可以在其中编写如下的逻辑。
之后,您可以在路线数组中进行以下更改这样。
可以在preectedRoute.js中添加其他功能,例如基于角色等。
You can create a ProtectedRoute.js file/folder where you can write the logic as follows.
After that you can do the following changes in routes array just like this.
Other functionalities could be added like role based etc. in the ProtectedRoute.js .