Regex 在特定路由上呈现组件-正则表达式路径
我试图仅在几个特定路径中呈现组件(标题),而不在其子路径中呈现,即Regex 在特定路由上呈现组件-正则表达式路径,regex,reactjs,react-router,react-router-v4,react-router-dom,Regex,Reactjs,React Router,React Router V4,React Router Dom,我试图仅在几个特定路径中呈现组件(标题),而不在其子路径中呈现,即/foo而不是/foo/bar中 假设我有一个路径数组: 让路径=['/foo','/bar','/baz'] 我试图这样做,但没有成功: const x = paths.map(e => `(${path}${e}/?$)`) <Route path={x.join('|')} component={this.makeMyComponent} /> cons
/foo
而不是/foo/bar
中
假设我有一个路径数组:
让路径=['/foo','/bar','/baz']
我试图这样做,但没有成功:
const x = paths.map(e => `(${path}${e}/?$)`)
<Route
path={x.join('|')}
component={this.makeMyComponent}
/>
const x=path.map(e=>`(${path}${e}/?$)`)
这也不起作用:
const x = paths.map(e => `(${e}/?$)`)
<Route
path={`${path}(/foo$|/bar$|/baz$})}
component={this.makeMyComponent}
/>
const x=path.map(e=>`(${e}/?$)`)
但它确实与在线正则表达式引擎匹配:
这起作用了:
<Route
render={props => paths.some(e => props.location.pathname.endsWith(e)) ? this.makeMyComponent() : null}
/>
path.some(e=>props.location.pathname.endsWith(e))?this.makeMyComponent():null}
/>