使用React Router Dom的URL路径级别

时间:2019-05-24 09:59:39

标签: javascript reactjs react-router react-router-dom

如何使用 react-router-dom

使用此路径admin/users/:id

当级别仅为1时我没问题,我的意思是:/users/:id

路由器:

class App extends Component {
  render() {
    return (
      <Router>
        <React.Fragment>
          <Header />
          <Container fluid={true} className="content">
            <Switch>
              <Route path="/dashboard" render={() => <h1>DashBoard</h1>} />
              <Route path="/admin/users" component={AdminUsers} />
              //Below line is not working as I expect.
              <Route path="/admin/users/:id" render={() => <h1>User View</h1>} />
              <Route path="/admin/clients" render={() => <h1>Admin Clients</h1>} />
              <Route path="/reporting" component={AdminReportPage} />
              <Route path="/help" render={() => <h1>Help</h1>} />
              <Route render={() => <h1>Page not found.</h1>} />
            </Switch>
          </Container>
        </React.Fragment>
      </Router>
    )
  }
}

export default App;

当我尝试转到admin/users/:id时,应用程序无响应并停留在admin/users中。

  

额外:“ react-router-dom”:“ 4.3.1”

1 个答案:

答案 0 :(得分:1)

输入“ / admin / users”的确切属性 即:

<Route exact path="/admin/users" component={AdminUsers} />