简体   繁体   English

使用useRoutes时如何实现基于路由的代码拆分

[英]How to implement route-based code splitting when using useRoutes

Am trying to find a way of implementing route-based code-splitting, I tried lazy loading the components and wrap the component in suspense.我试图找到一种实现基于路由的代码拆分的方法,我尝试延迟加载组件并将组件包装在悬念中。 Still, it resulted in only several components being successfully split from the main bundle and bu not all.尽管如此,它还是导致只有几个组件成功地从主包中拆分出来,而不是全部。

function AppRoutes() {
      const routes = useRoutes(
        [
          {path:'/',
           element:(<Suspense fallback={<Loading size={45} />}><Login/></Suspense>)},
          {path:'signup',
           element:(<Suspense fallback={<Loading size={45} />}><Signup/></Suspense>)},
          {path="about", 
           element:(<Suspense fallback={<Loading size={45} />}><About/></Suspense>) }
          {path="contact", 
           element:(<Suspense fallback={<Loading size={45} />}><Contact /></Suspense>)}
          {path="customers", 
           element:(<Suspense fallback={<Loading size={45} />}><Customers /></Suspense>)}
          {path="loans", 
           element:(<Suspense fallback={<Loading size={45} />}><Loans /></Suspense>}
          {path="stats", 
           element:(<Suspense fallback={<Loading size={45} />}><Statistics /></Suspense>}
          {path="dashboard", 
           element:(<Suspense fallback={<Loading size={45} />}><Dashboard/></Suspense> }
        ]
      )
      return routes;
    }

Use lazy loading for the components.对组件使用延迟加载。 It should not be nothing special应该没什么特别的

const Login = lazy(() => import('./Login').then(component => component))

That will split the file where you are calling useRoute into small chunks这会将您调用 useRoute 的文件分成小块

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 React 基于路由的代码拆分导入 Unexpected token 错误 - React Route-based code splitting import Unexpected token error 使用基于 React Route 的代码拆分进行全新构建后的白页 - White page after fresh build using React Route-based code splitting 使用useRoutes时如何将道具传递给路由组件? - How to pass props to route components when using useRoutes? 如何在 useRoutes 模式下重定向到子路由? - How to redirect to child route on useRoutes mode? 使用参数重定向时使用 useRoutes 时出现问题 - Problem using useRoutes when redirecting with params 尝试使用 React Route 和 Webpack 实现 React “Lazy” 路由以进行代码拆分 - Trying to implement a React “Lazy” route for code splitting with React Route and Webpack react-router 路由内基于路由的测试的推荐方法 - Recommended approach for route-based tests within routes of react-router Webpack 基于路由的代码拆分中的异常供应商块加载行为 - Unusual Vendor Chunk loading behaviour in Webpack route based code splitting 使用webpack的基于路由的代码拆分不起作用,未生成块 - Route based code splitting with webpack not working, Chunks not generated 如何使用 react-router 实现延迟加载和代码拆分? - How to implement lazy loading and code splitting with react-router?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM