【问题标题】:Rendering a component on specific routes - Regex path在特定路由上渲染组件 - 正则表达式路径
【发布时间】:2018-07-04 15:30:28
【问题描述】:

我正在尝试仅在几个特定路径中呈现组件(标题),而不是在它们的子路径中,即 /foo 而不是 /foo/bar

假设我有一个路径数组: let paths = ['/foo', '/bar', '/baz']

我尝试这样做,但没有成功:

    const x = paths.map(e => `(${path}${e}/?$)`)
    <Route
      path={x.join('|')}
      component={this.makeMyComponent}
    />

这也不起作用:

    const x = paths.map(e => `(${e}/?$)`)
    <Route
      path={`${path}(/foo$|/bar$|/baz$})}
      component={this.makeMyComponent}
    />

但它确实匹配在线正则表达式引擎:

【问题讨论】:

    标签: regex reactjs react-router react-router-v4 react-router-dom


    【解决方案1】:

    这行得通:

        <Route
          render={props => paths.some(e => props.location.pathname.endsWith(e)) ? this.makeMyComponent() : null}
        />
    

    【讨论】:

      猜你喜欢
      • 2018-04-18
      • 1970-01-01
      • 1970-01-01
      • 2019-08-19
      • 1970-01-01
      • 2019-07-22
      • 1970-01-01
      • 2018-12-20
      • 2023-04-03
      相关资源
      最近更新 更多