微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

react笔记 路由嵌套

如果你的项目需要很多个路由  传统的路由方式就显得十分臃肿

因此我们需要对路由模块化

import xxxrouter   记住  不要在import上面定义变量  会报错

let routes = [{
    path: "/",
    component: Home,
    exact: true
}, {
    path: "/News",
    component: News,
    exact: false
}, {
    path: "/",
    component: Home,
    exact: true
}, {
    path: "/",
    component: Home,
    exact: true
},]

class XXX

是不是对写法很熟悉?  没错  与vue中的路由模块化写法相同

我们需要在render 中循环遍历这个routes数组

      {
        routes.map((route,key)=>{
          if(route.exact){
            return <Route key={key} exact path={route.path} component={route.component}></Route>
          }else{
            return <Route key={key} path={route.path} component={route.component}></Route>
          }
        
        })
      }

这样就做到了路由的模块化

我们可以选择将routes封装为一个model 引入组件 随后将 export暴露出来 在需要的页面引入这个model

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐