鍍金池/ 問答/HTML5  HTML/ react-router-config嵌套之后不渲染,求解決。

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

圖片描述

路由配置文件結(jié)構(gòu)如此,為什么把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
回答
編輯回答
不討喜

首先你放在一個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頁面,后面的問題也同理。

2017年6月10日 01:11