路由到子路由模块,无需延迟加载 [英] Routing to sub routing module without lazy loading
问题描述
我想要多个 routing
模块,以保持我的应用程序干净且易于阅读.我目前对 SubComponent
使用延迟加载,但我不想这样做.所以我正在寻找一种方法来改变这种情况.无论如何,这是当前工作的代码.
I want to have multiple routing
modules in order to keep my application clean and easy to read. I currently use lazy loading for the SubComponent
but I don't want to do this. So I am looking for a way to change this. Anyways, here is the currently working code.
我有以下两个路由文件.
I have the following two routing files.
app-routing.module.ts
:
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
const routes: Routes = [
{ path: '', component: HomeComponent },
{ path: 'sub', loadChildren: './sub/sub.module#SubModule' }
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
sub-routing.module.ts
:
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
const routes: Routes = [
{ path: '', component: SubComponent, children: [
{ path: 'new', component: SubEditComponent }
] },
];
@NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule]
})
这种方式工作正常,但我不想对这个 SubComponent
应用延迟加载.所以,理想情况下,我想将 app-routing.module.ts
更改为:
It works fine this way but I don't want to apply lazy loading to this SubComponent
. So, ideally I want to change the app-routing.module.ts
to this:
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
const routes: Routes = [
{ path: '', component: HomeComponent },
{ path: 'sub', component: SubComponent }
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
这将不起作用并导致以下错误:
This will not work and result in the following error:
ERROR Error: Uncaught (in promise): Error: Cannot match any routes. URL Segment: 'sub/new'
Error: Cannot match any routes. URL Segment: 'sub/new'
SubComponent
的大小将大幅增长,出于自身原因,我不想应用延迟加载.那么无论如何,有没有办法在避免延迟加载的同时使用多个路由文件?
The SubComponent
will grow substantially in size and I don't want to apply lazy loading for my own reasons. So in any event, is there a way to use multiple routing files while avoiding lazy loading?
推荐答案
你有没有试过这样加载:
Have you tried loading it like this:
{ path: 'sub', loadChildren: () =>子模块 }
您可以在此处找到更多详细信息.
You can find a lot more details here.
这篇关于路由到子路由模块,无需延迟加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!