[英]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.