react-router 4中,路由无法直接访问,或者跳转

页面结构:
RootMap:

            <BrowserRouter history={this.props.history}>
              <Switch>
                    <Route exact path='/' component={Login}/>
                    <Route path='/login' component={Login}/>
                    <Route path='/home' component={Home}/>
                    <Route component={NotFound}/>
              </Switch>
            </BrowserRouter>

Home内结构:

      <BrowserRouter>
        <div>
          <Head/>
          <Aside/>
          <switch>
            <Route exact path='/home' component={Container}/>
            <Route path='/home/dashboards' component={Container}/>
            <Route path='/home/setedge' component={SetEdge}/>
            <Route path='/home/controls' component={Controls}/>
          </switch>
        </div>
      </BrowserRouter>
      

SetEdge内结构:

      <BrowserRouter>
        <Switch>
            <Route exact path='/home/setedge' component={SetIot}/>
            <Route path='/home/setedge/setiot' component={SetIot}/>
            <Route path='/home/setedge/setdevice' component={SetDevice}/>
          </Switch>
      </BrowserRouter>
      
      

页面结构如上,现在我能进入/home和/login,但是就是不能直接进入/home/setedge,子路由的所有路由全都不能进去,但是如果先进/home,再点击菜单中的跳转,就能进入/home/setedge

而且我现在如果在/home/setedge/setiot下,点击返回按钮,触发,this.props.history.push('/home/setedge');浏览器中路由变化了,但是页面回不去。没效果

问题一,我改怎么写路由,才能我随意输入一个路由就能进去,不用一层一层的进去,先进/home,再点击菜单中的跳转,进入/home/setedge,而是直接浏览器路由填写/home/setedge进入。

问题二,我该怎么设置页面跳转,才能让路由生效

阅读 8k
5 个回答

只保留最外层的一个 BrowserRouter ,你再试试

我也遇到了 楼主解决了吗 直接访问不行 只能通过连接点击,而且点击时明明url已经改变了

新手上路,请多包涵

Nginx 增加

location / {
    try_files $uri /index.html;
}

让nginx向index.html请求

推荐问题