在Angular 7中,路由解析器加载组件的行为是默认的。如果您想要避免加载组件,可以通过以下方法进行解决:
loadChildren
属性加载延迟加载的模块:const routes: Routes = [
{
path: 'lazy',
loadChildren: './lazy-module/lazy-module.module#LazyModule'
}
];
在这个示例中,当路由到/lazy
路径时,Angular将加载LazyModule
模块,而不会立即加载组件。
componentless
路由创建一个没有组件的路由:const routes: Routes = [
{
path: 'no-component',
component: NoComponentRouteComponent,
children: [
{
path: 'child',
component: ChildComponent
}
]
}
];
在这个示例中,当路由到/no-component/child
路径时,Angular将加载ChildComponent
组件,但是NoComponentRouteComponent
组件将不会被加载。
请注意,这只是一种解决方法,具体取决于您的需求和项目结构。