我在一个简单的应用程序,只包含4个模块login/registration,accounts,account-typeswebsites.我已经为authlayout创建了2个模块.

所有经过身份验证的模块都共享<Layout />布局.

...

<Route path="/" element={<Auth />}>
  <Route {...routes.getRoute('auth')} />
  <Route {...routes.getRoute('register')} />
</Route>

<Route path="/accounts" element={<Layout />}>
  <Route {...routes.getRoute('accounts')} />
  <Route {...routes.getRoute('accountCreate')} />
  <Route {...routes.getRoute('accountEdit')} />
</Route>

<Route path="/account-types" element={<Layout />}>
  <Route {...routes.getRoute('accountTypes')} />
  <Route {...routes.getRoute('accountTypeCreate')} />
  <Route {...routes.getRoute('accountTypeEdit')} />
</Route>

<Route path="/websites" element={<Layout />}>
  <Route {...routes.getRoute('websites')} />
  <Route {...routes.getRoute('websiteCreate')} />
  <Route {...routes.getRoute('websiteEdit')} />
</Route>
...

这段代码可以缩短吗,大概是这样的:

<Route path="/" element={<Auth />}>
  <Route {...routes.getRoute('auth')} />
  <Route {...routes.getRoute('register')} />
</Route>

<Route path={["/accounts", "/account-types", "/websites"]} element={<Layout />}>
  <Route {...routes.getRoute('accounts')} />
  <Route {...routes.getRoute('accountCreate')} />
  <Route {...routes.getRoute('accountEdit')} />

  <Route {...routes.getRoute('accountTypes')} />
  <Route {...routes.getRoute('accountTypeCreate')} />
  <Route {...routes.getRoute('accountTypeEdit')} />

  <Route {...routes.getRoute('websites')} />
  <Route {...routes.getRoute('websiteCreate')} />
  <Route {...routes.getRoute('websiteEdit')} />
</Route>

我try 了以下方法,但收到了很多错误:

{["/accounts", "/account-types", "/websites"].map(path => (
  <Route path={path} element={<Layout />}>
    <Route {...routes.getRoute('accounts')} />
    <Route {...routes.getRoute('accountCreate')} />
    <Route {...routes.getRoute('accountEdit')} />

    <Route {...routes.getRoute('accountTypes')} />
    <Route {...routes.getRoute('accountTypeCreate')} />
    <Route {...routes.getRoute('accountTypeEdit')} />

    <Route {...routes.getRoute('websites')} />
    <Route {...routes.getRoute('websiteCreate')} />
    <Route {...routes.getRoute('websiteEdit')} />
  </Route>
))}

推荐答案

Route路径只接受字符串路径值,因此传递字符串路径数组是无效的.如果您真正想要完成的只是移除重复的Layout组件渲染,则可以从子布线路径中拆分Layout布局布线.渲染单个Layout布局布线,并为"/accounts"/account-types""/websites"中的每一个渲染嵌套布局布线.

示例:

...

<Route path="/" element={<Auth />}>
  <Route {...routes.getRoute('auth')} />
  <Route {...routes.getRoute('register')} />
</Route>

<Route element={<Layout />}>
  <Route path="/accounts">
    <Route {...routes.getRoute('accounts')} />
    <Route {...routes.getRoute('accountCreate')} />
    <Route {...routes.getRoute('accountEdit')} />
  </Route>
  <Route path="/account-types">
    <Route {...routes.getRoute('accountTypes')} />
    <Route {...routes.getRoute('accountTypeCreate')} />
    <Route {...routes.getRoute('accountTypeEdit')} />
  </Route>
  <Route path="/websites">
    <Route {...routes.getRoute('websites')} />
    <Route {...routes.getRoute('websiteCreate')} />
    <Route {...routes.getRoute('websiteEdit')} />
  </Route>
</Route>
...

如果您试图进一步减少代码"混乱",那么您还可以重构代码以映射嵌套的路由.

const renderRoute = (routeKey) => (
  <Route key={routeKey} {...routes.getRoute(routeKey)} />
);
const renderRoutes = routes => routes.map(renderRoute);
...

<Route path="/" element={<Auth />}>
  {renderRoutes(["auth", "register"])}
</Route>

<Route element={<Layout />}>
  <Route path="/accounts">
    {renderRoutes(["accounts", "accountCreate", "accountEdit"])}
  </Route>
  <Route path="/account-types">
    {renderRoutes(["accountTypes", "accountTypeCreate", "accountAccountEdit"])}
  </Route>
  <Route path="/websites">
    {renderRoutes(["websites", "websiteCreate", "websiteEdit"])}
  </Route>
</Route>
...

Javascript相关问答推荐

无法在page. evalve()内部使用外部函数

如何避免使用ajax在Vue 3合成API中重定向

我应该在redux reducer中调用其他reducer函数吗?

zoom svg以适应圆

如何在RTK上设置轮询,每24小时

基于变量切换隐藏文本

如何在angular中从JSON值添加动态路由保护?

用JavaScript复制C#CRC 32生成器

查找最长的子序列-无法重置数组

如何使用JavaScript拆分带空格的单词

WP Bootstrap NavWaker:下拉菜单一次打开所有下拉菜单

当我try 将值更改为True时,按钮不会锁定

如果没有页面重新加载Angular ,innerHTML属性绑定不会更新

按什么顺序接收`storage`事件?

无法重定向到Next.js中的动态URL

如何在一个对象Java脚本中获取不同键的重复值?

如果NetSuite中为空,则限制筛选

为什么这个最小Angular 的Licial.dev设置不起作用?

如何将对象推送到firestore数组?

使用静态函数保存 node 前的钩子