react-router-config嵌套之后不渲染,求解决。

发布于 2022-09-06 15:31:13 字数 1620 浏览 7 评论 0

图片描述

路由配置文件结构如此,为什么把login组件放在最下面不会渲染呢?
而且如果有两个子组件同时拥有孙子组件的话,也只会渲染一个呢。

    const App = () => ( //app
    <Router>
    <Switch>
      {renderRoutes(configRoutes)}
    </Switch>
    </Router>)
    export default App
const render = Component => {
  ReactDOM.render(
    <AppContainer warnings={false}>
      <Provider store={store}>
        <Component />
      </Provider>
    </AppContainer>,
    document.getElementById('root')
  )
}

render(App)
const basicRoutes = [   //路由页面
  // { // 放在上面渲染OK,
  //   path: '/login',
  //   component: Login
  // },
  {
    component: HomeView,
    routes: [
      {
        path: '/',
        exact: true,
        component: Home
      },
      {
        path: '/page1',
        exact: true,

        component: Page1
      },
      // { // 渲染OK
      //   path: '/login',
      //   component: Login
      // },
      {
        component: Page2,
        routes: [
          {

            path: '/page2/:id',
            exact: true,

            component: Page4
          },
          {
            path: '/page4',
            exact: true,

            component: Page1
          }
        ]
      }
      // { // 渲染失败
      //   path: '/login',
      //   component: Login
      // }
    ]
  },
  // { // 渲染失败
  //   component: Login,
  //   exact: true,
  //   path: '/login'
  // }
]

export default basicRoutes

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

別甾虛僞 2022-09-13 15:31:13

首先你放在一个Switch里面,所以只会有一条路线会命中。

<Switch> is unique in that it renders a route exclusively. In contrast, every <Route> that matches the location renders inclusively

其次HomeView组件以及Page2组件的path都为空,所以它会命中任何的路由,所以如果你把Login放在后面,因为前面永远都有路由命中,自然不会渲染Login页面,后面的问题也同理。

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文