【问题标题】:reactJS url params name stay in url when are optionnal反应 JS url 参数名称在可选时保留在 url 中
【发布时间】:2020-11-26 15:55:27
【问题描述】:

我是 ReactJS 的初学者,我在 url 中的可选参数有问题。

我使用 react-router-dom 5.1.2。

当我尝试使用具有可选参数的组件创建路由时,如果没有参数值,则此路由将保留参数名称。如果我创建一个带参数的链接一切正常。

例如在我的侧边栏中,访问 AudioComponent 的路径是(链接是用路由组件生成的):

<a class="sidebar-link" href="/callcenter/soundplayer/:agentName?/:date?">Audios</a>

我的组件路由:

{
  path: "/callcenter/soundplayer/:agentName?/:date?",
  name: "Audios",
  component: SoundFileRow,
  isShow: true,
  access: "staffAccess"
},

还有我的 Routes.js(我下载了一个模板,所以 Route 代码已经是这样了,除了条件)

const childRoutes = (Layout, routes, navBarAccess) =>   routes.map(({ children, path, realPath, component: Component, access }, index) =>
    children ? (
      // Route item with children
      children.map(({ path, component: Component , access}, index) => (
        <Route
          key={index}
          path={path}
          exact
          render={props => (
            (checkIsAuth())? 
            checkHasAccess(access) ? 
            <Layout>
              <Component {...props} />
            </Layout> :
            <AuthLayout>
              <Page404 />
            </AuthLayout> :
            <Redirect to={{ pathname: '/auth/sign-in' }} />
          )}
        />
      ))
    ) : (
      // Route item without children
      <Route
        key={index}
        path={path}
        exact
        render={props => (
          (checkIsAuth()) ? 
          (checkHasAccess(access)) ? 
            <Layout>
              <Component {...props} />
            </Layout> : 
            <AuthLayout>
              <Page404 />
            </AuthLayout> :
            (path !== "/auth/sign-in") ? 
            <Redirect to={{ pathname: '/auth/sign-in' }} /> : 
            <Layout>
              <Component {...props}  />
            </Layout>
        )}
      />
    )   );

和我的渲染:

render() {
       return (   <Router>
     <ScrollToTop>
       <Switch>
         {childRoutes(DashboardLayout, dashboardRoutes)}
         {childRoutes(DashboardLayout, defaultRoutes)}
         {childRoutes(AuthLayout, signInRoutes)}
         <Route
           render={() => (
             (checkIsAuth()) ? 
             <AuthLayout>
               <Page404 />
             </AuthLayout> : 
             <Redirect to={{ pathname: '/auth/sign-in' }} />
           )}
         />
       </Switch>
     </ScrollToTop>   </Router> );   }

有人已经有这个问题了吗? 提前感谢您的帮助:)

【问题讨论】:

  • 有人遇到过同样的问题吗?

标签: reactjs router react-router-dom


【解决方案1】:

我解决了。问题出在模板使用相同的路径来了解 url 参数并在 href 中显示 url。我只是在我的组件中创建了一个新参数:

{
      path: "/callcenter/soundplayer",
      realPath: "/callcenter/soundplayer/:agentName?/:date?",
      name: "Audios",
      component: SoundFileRow,
      isShow: true,
      access: "staffAccess"
    },

然后我将它添加到我的 router.js 中:

    const childRoutes = (Layout, routes, navBarAccess) =>
routes.map(({ children, path, realPath, component: Component, access }, index) =>
  children ? (
      // Route item with children
      children.map(({ path, realPath, component: Component , access}, index) => (
        <Route
        key={index}
        path={realPath}
        exact
        render={props => (
          (checkIsAuth())? 
          checkHasAccess(access) ? 
          <Layout>
          <Component {...props} />
          </Layout> :
          <AuthLayout>
          <Page404 />
          </AuthLayout> :
          <Redirect to={{ pathname: '/auth/sign-in' }} />
          )}
        />
        ))
      ) : (
      // Route item without children
      <Route
      key={index}
      path={realPath}
      exact
      render={props => (
        (checkIsAuth()) ? 
        (checkHasAccess(access)) ? 
        <Layout>
        <Component {...props} />
        </Layout> : 
        <AuthLayout>
        <Page404 />
        </AuthLayout> :
        (path !== "/auth/sign-in") ? 
        <Redirect to={{ pathname: '/auth/sign-in' }} /> : 
        <Layout>
        <Component {...props}  />
        </Layout>
        )}
        />
        )
      );

希望今天对某人有用!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多