【发布时间】: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