• react


    routerCofing配置

    
      {
        path: '/route1/someModel',
        children: [
          {
            path: '/route2',
            component: RouteBase.Cps1,
            children: [
              {
                path: '/route3',
                component: RouteBase.Cps2,
                children: [
                  {
                    path: '/route4',
                    component: RouteBase.Cps3,
                  },
                ]
              },
            ]
          },
          {
            path: '/route5', // 退货
            component: RouteBase.Cps4
          }
        ]
      },
    
    

    router - index

      // 返回路由
      const RouteItem = props => {
        const { redirect, path, component, key } = props
        if (redirect) {
          return <Redirect exact key={key} from={path} to={redirect} />
        }
        return <Route exact key={key} component={component} path={path} />
      }
    
      // eslint-disable-next-line no-array-constructor
      let Routes: any = new Array()
    
      // 获取子路由
    
      const loopRoute = (route, i, pre_path?: string) => {
        return route.children.forEach((routeChild, idx) => {
        let __path = pre_path + routeChild.path
          const { redirect, component, children } = routeChild
          if (children && children.length) {
            // 递归获取子路径
            if (component) {
              Routes = Routes.flat()
              Routes.push(RouteItem({
                key: `${i}-${idx}`,
                redirect,
                path: __path,
                component: component
              }))
            }
            loopRoute(routeChild, idx, __path)
          } else {
            Routes.push( RouteItem({
              key: `${i}-${idx}`,
              redirect,
              path: __path,
              component: component
            }))
          }
        })
      }
    
      routes.forEach((route: any, key) => {
        return Array.isArray(route.children) && route.children.length
          ?  loopRoute(route, key, route.path)
          :  Routes.push(RouteItem({ key, ...route }))
      })
    
    // 使用
    
    <Switch>
      <Route exact path='/login' component={Login} />
      {Routes}
    </Switch>
    
  • 相关阅读:
    添加事件(jquery)
    闭包导致的问题
    学习之js绑定事件
    第二条 一个类如果有多个参数,考虑用Builder构造者模式
    用Intellij IDEA 创建第一个maven项目!
    OrderSessionHelper查看订单在session是否存在的辅助类
    css——overflow
    css——盒子
    css——外部样式
    css——权重叠加
  • 原文地址:https://www.cnblogs.com/mapleChain/p/12632719.html
Copyright © 2020-2023  润新知