Angular 不显示深嵌套组件
我在显示第三个嵌套组件时遇到问题 预期: 你好应用程序组件 Hello Nest-A组件 Hello Nest-1组件 Hello Test-Z组件 实际: 你好应用程序组件 Hello Nest-A组件 Hello Nest-1组件 为什么我的Test-Z组件不显示 TLDR 应用程序模块.tsAngular 不显示深嵌套组件,angular,typescript,angular-routing,Angular,Typescript,Angular Routing,我在显示第三个嵌套组件时遇到问题 预期: 你好应用程序组件 Hello Nest-A组件 Hello Nest-1组件 Hello Test-Z组件 实际: 你好应用程序组件 Hello Nest-A组件 Hello Nest-1组件 为什么我的Test-Z组件不显示 TLDR 应用程序模块.ts import { NgModule } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { RouterModule, Routes } from '@angular/router';
import { AppComponent } from './app.component';
import { NestAModule } from './nest-a/nest-a.module';
const rootRoutes: Routes = [
{ path: '', redirectTo: 'nest-a', pathMatch: 'full' },
{ path: 'nest-a', redirectTo: 'nest-a', pathMatch: 'full' },
];
@NgModule({
imports: [
BrowserModule,
FormsModule,
RouterModule.forRoot(rootRoutes),
NestAModule,
],
declarations: [
AppComponent,
],
bootstrap: [
AppComponent
]
})
export class AppModule { }
import { Component } from '@angular/core';
@Component({
selector: 'my-app',
template: '<h1>Hello App Component</h1><router-outlet></router-outlet>',
})
export class AppComponent {
}
import { NgModule } from '@angular/core';
import { RouterModule } from '@angular/router';
import { NestAComponent } from './nest-a.component';
import { Nest1Component } from './nest-1/nest-1.component';
export const nestARoutes = [
{
title: 'Nest A',
path: 'nest-a',
component: NestAComponent,
children: [
{ path: '', redirectTo: 'nest-1', pathMatch: 'full' },
{ path: 'nest-1', component: Nest1Component },
],
},
];
@NgModule({
imports: [
RouterModule.forChild(nestARoutes),
],
exports: [
RouterModule
]
})
export class NestARoutingModule { }
import { Component } from '@angular/core';
@Component({
selector: 'my-nest-a',
template: `<h1>Hello Nest-A Component</h1><router-outlet></router-outlet>`
})
export class NestAComponent {
}
import { NgModule } from '@angular/core';
import { RouterModule } from '@angular/router';
import { NestAComponent } from './nest-a.component';
import { NestARoutingModule, nestARoutes } from './nest-a-routing.module';
import { Nest1Module } from './nest-1/nest-1.module';
@NgModule({
declarations: [
NestAComponent,
],
imports: [
NestARoutingModule,
Nest1Module,
RouterModule.forChild(nestARoutes),
],
})
export class NestAModule { }
import { NgModule } from '@angular/core';
import { RouterModule } from '@angular/router';
import { Nest1Component } from './nest-1.component';
import { TestYComponent } from './test-y/test-y.component'
import { TestZComponent } from './test-z/test-z.component'
export const nest1Routes = [
{
title: 'Nest 1',
path: 'nest-1',
component: Nest1Component,
children: [
{ path: '', redirectTo: 'test-z', pathMatch: 'full'},
{ path: 'test-y', component: TestYComponent},
{ path: 'test-z', component: TestZComponent},
]
},
];
@NgModule({
imports: [
RouterModule.forChild(nest1Routes),
],
exports: [
RouterModule
]
})
export class Nest1RoutingModule { }
import { Component } from '@angular/core';
@Component({
selector: 'my-nest-1',
template: `<h1>Hello Nest-1 Component</h1><router-outlet></router-outlet>`
})
export class Nest1Component {
}
import { NgModule } from '@angular/core';
import { RouterModule } from '@angular/router';
import { Nest1Component } from './nest-1.component';
import { Nest1RoutingModule, nest1Routes } from './nest-1-routing.module';
import { TestZModule } from './test-z/test-z.module'
@NgModule({
declarations: [
Nest1Component,
],
imports: [
Nest1RoutingModule,
TestZModule,
RouterModule.forChild(nest1Routes),
],
})
export class Nest1Module { }
应用程序组件.ts
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { RouterModule, Routes } from '@angular/router';
import { AppComponent } from './app.component';
import { NestAModule } from './nest-a/nest-a.module';
const rootRoutes: Routes = [
{ path: '', redirectTo: 'nest-a', pathMatch: 'full' },
{ path: 'nest-a', redirectTo: 'nest-a', pathMatch: 'full' },
];
@NgModule({
imports: [
BrowserModule,
FormsModule,
RouterModule.forRoot(rootRoutes),
NestAModule,
],
declarations: [
AppComponent,
],
bootstrap: [
AppComponent
]
})
export class AppModule { }
import { Component } from '@angular/core';
@Component({
selector: 'my-app',
template: '<h1>Hello App Component</h1><router-outlet></router-outlet>',
})
export class AppComponent {
}
import { NgModule } from '@angular/core';
import { RouterModule } from '@angular/router';
import { NestAComponent } from './nest-a.component';
import { Nest1Component } from './nest-1/nest-1.component';
export const nestARoutes = [
{
title: 'Nest A',
path: 'nest-a',
component: NestAComponent,
children: [
{ path: '', redirectTo: 'nest-1', pathMatch: 'full' },
{ path: 'nest-1', component: Nest1Component },
],
},
];
@NgModule({
imports: [
RouterModule.forChild(nestARoutes),
],
exports: [
RouterModule
]
})
export class NestARoutingModule { }
import { Component } from '@angular/core';
@Component({
selector: 'my-nest-a',
template: `<h1>Hello Nest-A Component</h1><router-outlet></router-outlet>`
})
export class NestAComponent {
}
import { NgModule } from '@angular/core';
import { RouterModule } from '@angular/router';
import { NestAComponent } from './nest-a.component';
import { NestARoutingModule, nestARoutes } from './nest-a-routing.module';
import { Nest1Module } from './nest-1/nest-1.module';
@NgModule({
declarations: [
NestAComponent,
],
imports: [
NestARoutingModule,
Nest1Module,
RouterModule.forChild(nestARoutes),
],
})
export class NestAModule { }
import { NgModule } from '@angular/core';
import { RouterModule } from '@angular/router';
import { Nest1Component } from './nest-1.component';
import { TestYComponent } from './test-y/test-y.component'
import { TestZComponent } from './test-z/test-z.component'
export const nest1Routes = [
{
title: 'Nest 1',
path: 'nest-1',
component: Nest1Component,
children: [
{ path: '', redirectTo: 'test-z', pathMatch: 'full'},
{ path: 'test-y', component: TestYComponent},
{ path: 'test-z', component: TestZComponent},
]
},
];
@NgModule({
imports: [
RouterModule.forChild(nest1Routes),
],
exports: [
RouterModule
]
})
export class Nest1RoutingModule { }
import { Component } from '@angular/core';
@Component({
selector: 'my-nest-1',
template: `<h1>Hello Nest-1 Component</h1><router-outlet></router-outlet>`
})
export class Nest1Component {
}
import { NgModule } from '@angular/core';
import { RouterModule } from '@angular/router';
import { Nest1Component } from './nest-1.component';
import { Nest1RoutingModule, nest1Routes } from './nest-1-routing.module';
import { TestZModule } from './test-z/test-z.module'
@NgModule({
declarations: [
Nest1Component,
],
imports: [
Nest1RoutingModule,
TestZModule,
RouterModule.forChild(nest1Routes),
],
})
export class Nest1Module { }
nest-a/nest-a.component.ts
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { RouterModule, Routes } from '@angular/router';
import { AppComponent } from './app.component';
import { NestAModule } from './nest-a/nest-a.module';
const rootRoutes: Routes = [
{ path: '', redirectTo: 'nest-a', pathMatch: 'full' },
{ path: 'nest-a', redirectTo: 'nest-a', pathMatch: 'full' },
];
@NgModule({
imports: [
BrowserModule,
FormsModule,
RouterModule.forRoot(rootRoutes),
NestAModule,
],
declarations: [
AppComponent,
],
bootstrap: [
AppComponent
]
})
export class AppModule { }
import { Component } from '@angular/core';
@Component({
selector: 'my-app',
template: '<h1>Hello App Component</h1><router-outlet></router-outlet>',
})
export class AppComponent {
}
import { NgModule } from '@angular/core';
import { RouterModule } from '@angular/router';
import { NestAComponent } from './nest-a.component';
import { Nest1Component } from './nest-1/nest-1.component';
export const nestARoutes = [
{
title: 'Nest A',
path: 'nest-a',
component: NestAComponent,
children: [
{ path: '', redirectTo: 'nest-1', pathMatch: 'full' },
{ path: 'nest-1', component: Nest1Component },
],
},
];
@NgModule({
imports: [
RouterModule.forChild(nestARoutes),
],
exports: [
RouterModule
]
})
export class NestARoutingModule { }
import { Component } from '@angular/core';
@Component({
selector: 'my-nest-a',
template: `<h1>Hello Nest-A Component</h1><router-outlet></router-outlet>`
})
export class NestAComponent {
}
import { NgModule } from '@angular/core';
import { RouterModule } from '@angular/router';
import { NestAComponent } from './nest-a.component';
import { NestARoutingModule, nestARoutes } from './nest-a-routing.module';
import { Nest1Module } from './nest-1/nest-1.module';
@NgModule({
declarations: [
NestAComponent,
],
imports: [
NestARoutingModule,
Nest1Module,
RouterModule.forChild(nestARoutes),
],
})
export class NestAModule { }
import { NgModule } from '@angular/core';
import { RouterModule } from '@angular/router';
import { Nest1Component } from './nest-1.component';
import { TestYComponent } from './test-y/test-y.component'
import { TestZComponent } from './test-z/test-z.component'
export const nest1Routes = [
{
title: 'Nest 1',
path: 'nest-1',
component: Nest1Component,
children: [
{ path: '', redirectTo: 'test-z', pathMatch: 'full'},
{ path: 'test-y', component: TestYComponent},
{ path: 'test-z', component: TestZComponent},
]
},
];
@NgModule({
imports: [
RouterModule.forChild(nest1Routes),
],
exports: [
RouterModule
]
})
export class Nest1RoutingModule { }
import { Component } from '@angular/core';
@Component({
selector: 'my-nest-1',
template: `<h1>Hello Nest-1 Component</h1><router-outlet></router-outlet>`
})
export class Nest1Component {
}
import { NgModule } from '@angular/core';
import { RouterModule } from '@angular/router';
import { Nest1Component } from './nest-1.component';
import { Nest1RoutingModule, nest1Routes } from './nest-1-routing.module';
import { TestZModule } from './test-z/test-z.module'
@NgModule({
declarations: [
Nest1Component,
],
imports: [
Nest1RoutingModule,
TestZModule,
RouterModule.forChild(nest1Routes),
],
})
export class Nest1Module { }
nest-a/nest-1/nest-1-routing.module.ts
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { RouterModule, Routes } from '@angular/router';
import { AppComponent } from './app.component';
import { NestAModule } from './nest-a/nest-a.module';
const rootRoutes: Routes = [
{ path: '', redirectTo: 'nest-a', pathMatch: 'full' },
{ path: 'nest-a', redirectTo: 'nest-a', pathMatch: 'full' },
];
@NgModule({
imports: [
BrowserModule,
FormsModule,
RouterModule.forRoot(rootRoutes),
NestAModule,
],
declarations: [
AppComponent,
],
bootstrap: [
AppComponent
]
})
export class AppModule { }
import { Component } from '@angular/core';
@Component({
selector: 'my-app',
template: '<h1>Hello App Component</h1><router-outlet></router-outlet>',
})
export class AppComponent {
}
import { NgModule } from '@angular/core';
import { RouterModule } from '@angular/router';
import { NestAComponent } from './nest-a.component';
import { Nest1Component } from './nest-1/nest-1.component';
export const nestARoutes = [
{
title: 'Nest A',
path: 'nest-a',
component: NestAComponent,
children: [
{ path: '', redirectTo: 'nest-1', pathMatch: 'full' },
{ path: 'nest-1', component: Nest1Component },
],
},
];
@NgModule({
imports: [
RouterModule.forChild(nestARoutes),
],
exports: [
RouterModule
]
})
export class NestARoutingModule { }
import { Component } from '@angular/core';
@Component({
selector: 'my-nest-a',
template: `<h1>Hello Nest-A Component</h1><router-outlet></router-outlet>`
})
export class NestAComponent {
}
import { NgModule } from '@angular/core';
import { RouterModule } from '@angular/router';
import { NestAComponent } from './nest-a.component';
import { NestARoutingModule, nestARoutes } from './nest-a-routing.module';
import { Nest1Module } from './nest-1/nest-1.module';
@NgModule({
declarations: [
NestAComponent,
],
imports: [
NestARoutingModule,
Nest1Module,
RouterModule.forChild(nestARoutes),
],
})
export class NestAModule { }
import { NgModule } from '@angular/core';
import { RouterModule } from '@angular/router';
import { Nest1Component } from './nest-1.component';
import { TestYComponent } from './test-y/test-y.component'
import { TestZComponent } from './test-z/test-z.component'
export const nest1Routes = [
{
title: 'Nest 1',
path: 'nest-1',
component: Nest1Component,
children: [
{ path: '', redirectTo: 'test-z', pathMatch: 'full'},
{ path: 'test-y', component: TestYComponent},
{ path: 'test-z', component: TestZComponent},
]
},
];
@NgModule({
imports: [
RouterModule.forChild(nest1Routes),
],
exports: [
RouterModule
]
})
export class Nest1RoutingModule { }
import { Component } from '@angular/core';
@Component({
selector: 'my-nest-1',
template: `<h1>Hello Nest-1 Component</h1><router-outlet></router-outlet>`
})
export class Nest1Component {
}
import { NgModule } from '@angular/core';
import { RouterModule } from '@angular/router';
import { Nest1Component } from './nest-1.component';
import { Nest1RoutingModule, nest1Routes } from './nest-1-routing.module';
import { TestZModule } from './test-z/test-z.module'
@NgModule({
declarations: [
Nest1Component,
],
imports: [
Nest1RoutingModule,
TestZModule,
RouterModule.forChild(nest1Routes),
],
})
export class Nest1Module { }
nest-a/nest-1/nest-1.component.ts
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { RouterModule, Routes } from '@angular/router';
import { AppComponent } from './app.component';
import { NestAModule } from './nest-a/nest-a.module';
const rootRoutes: Routes = [
{ path: '', redirectTo: 'nest-a', pathMatch: 'full' },
{ path: 'nest-a', redirectTo: 'nest-a', pathMatch: 'full' },
];
@NgModule({
imports: [
BrowserModule,
FormsModule,
RouterModule.forRoot(rootRoutes),
NestAModule,
],
declarations: [
AppComponent,
],
bootstrap: [
AppComponent
]
})
export class AppModule { }
import { Component } from '@angular/core';
@Component({
selector: 'my-app',
template: '<h1>Hello App Component</h1><router-outlet></router-outlet>',
})
export class AppComponent {
}
import { NgModule } from '@angular/core';
import { RouterModule } from '@angular/router';
import { NestAComponent } from './nest-a.component';
import { Nest1Component } from './nest-1/nest-1.component';
export const nestARoutes = [
{
title: 'Nest A',
path: 'nest-a',
component: NestAComponent,
children: [
{ path: '', redirectTo: 'nest-1', pathMatch: 'full' },
{ path: 'nest-1', component: Nest1Component },
],
},
];
@NgModule({
imports: [
RouterModule.forChild(nestARoutes),
],
exports: [
RouterModule
]
})
export class NestARoutingModule { }
import { Component } from '@angular/core';
@Component({
selector: 'my-nest-a',
template: `<h1>Hello Nest-A Component</h1><router-outlet></router-outlet>`
})
export class NestAComponent {
}
import { NgModule } from '@angular/core';
import { RouterModule } from '@angular/router';
import { NestAComponent } from './nest-a.component';
import { NestARoutingModule, nestARoutes } from './nest-a-routing.module';
import { Nest1Module } from './nest-1/nest-1.module';
@NgModule({
declarations: [
NestAComponent,
],
imports: [
NestARoutingModule,
Nest1Module,
RouterModule.forChild(nestARoutes),
],
})
export class NestAModule { }
import { NgModule } from '@angular/core';
import { RouterModule } from '@angular/router';
import { Nest1Component } from './nest-1.component';
import { TestYComponent } from './test-y/test-y.component'
import { TestZComponent } from './test-z/test-z.component'
export const nest1Routes = [
{
title: 'Nest 1',
path: 'nest-1',
component: Nest1Component,
children: [
{ path: '', redirectTo: 'test-z', pathMatch: 'full'},
{ path: 'test-y', component: TestYComponent},
{ path: 'test-z', component: TestZComponent},
]
},
];
@NgModule({
imports: [
RouterModule.forChild(nest1Routes),
],
exports: [
RouterModule
]
})
export class Nest1RoutingModule { }
import { Component } from '@angular/core';
@Component({
selector: 'my-nest-1',
template: `<h1>Hello Nest-1 Component</h1><router-outlet></router-outlet>`
})
export class Nest1Component {
}
import { NgModule } from '@angular/core';
import { RouterModule } from '@angular/router';
import { Nest1Component } from './nest-1.component';
import { Nest1RoutingModule, nest1Routes } from './nest-1-routing.module';
import { TestZModule } from './test-z/test-z.module'
@NgModule({
declarations: [
Nest1Component,
],
imports: [
Nest1RoutingModule,
TestZModule,
RouterModule.forChild(nest1Routes),
],
})
export class Nest1Module { }
nest-a/nest-1/nest-/nest-.component.ts
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { RouterModule, Routes } from '@angular/router';
import { AppComponent } from './app.component';
import { NestAModule } from './nest-a/nest-a.module';
const rootRoutes: Routes = [
{ path: '', redirectTo: 'nest-a', pathMatch: 'full' },
{ path: 'nest-a', redirectTo: 'nest-a', pathMatch: 'full' },
];
@NgModule({
imports: [
BrowserModule,
FormsModule,
RouterModule.forRoot(rootRoutes),
NestAModule,
],
declarations: [
AppComponent,
],
bootstrap: [
AppComponent
]
})
export class AppModule { }
import { Component } from '@angular/core';
@Component({
selector: 'my-app',
template: '<h1>Hello App Component</h1><router-outlet></router-outlet>',
})
export class AppComponent {
}
import { NgModule } from '@angular/core';
import { RouterModule } from '@angular/router';
import { NestAComponent } from './nest-a.component';
import { Nest1Component } from './nest-1/nest-1.component';
export const nestARoutes = [
{
title: 'Nest A',
path: 'nest-a',
component: NestAComponent,
children: [
{ path: '', redirectTo: 'nest-1', pathMatch: 'full' },
{ path: 'nest-1', component: Nest1Component },
],
},
];
@NgModule({
imports: [
RouterModule.forChild(nestARoutes),
],
exports: [
RouterModule
]
})
export class NestARoutingModule { }
import { Component } from '@angular/core';
@Component({
selector: 'my-nest-a',
template: `<h1>Hello Nest-A Component</h1><router-outlet></router-outlet>`
})
export class NestAComponent {
}
import { NgModule } from '@angular/core';
import { RouterModule } from '@angular/router';
import { NestAComponent } from './nest-a.component';
import { NestARoutingModule, nestARoutes } from './nest-a-routing.module';
import { Nest1Module } from './nest-1/nest-1.module';
@NgModule({
declarations: [
NestAComponent,
],
imports: [
NestARoutingModule,
Nest1Module,
RouterModule.forChild(nestARoutes),
],
})
export class NestAModule { }
import { NgModule } from '@angular/core';
import { RouterModule } from '@angular/router';
import { Nest1Component } from './nest-1.component';
import { TestYComponent } from './test-y/test-y.component'
import { TestZComponent } from './test-z/test-z.component'
export const nest1Routes = [
{
title: 'Nest 1',
path: 'nest-1',
component: Nest1Component,
children: [
{ path: '', redirectTo: 'test-z', pathMatch: 'full'},
{ path: 'test-y', component: TestYComponent},
{ path: 'test-z', component: TestZComponent},
]
},
];
@NgModule({
imports: [
RouterModule.forChild(nest1Routes),
],
exports: [
RouterModule
]
})
export class Nest1RoutingModule { }
import { Component } from '@angular/core';
@Component({
selector: 'my-nest-1',
template: `<h1>Hello Nest-1 Component</h1><router-outlet></router-outlet>`
})
export class Nest1Component {
}
import { NgModule } from '@angular/core';
import { RouterModule } from '@angular/router';
import { Nest1Component } from './nest-1.component';
import { Nest1RoutingModule, nest1Routes } from './nest-1-routing.module';
import { TestZModule } from './test-z/test-z.module'
@NgModule({
declarations: [
Nest1Component,
],
imports: [
Nest1RoutingModule,
TestZModule,
RouterModule.forChild(nest1Routes),
],
})
export class Nest1Module { }
(有一个Y和Z,它们没有重要区别)
从'@angular/core'导入{Component};
@组成部分({
选择器:“我的测试-*”,
模板:`Hello Test-*组件`
})
导出类测试*组件{
}
您的代码中有几处错误,会给路由器的分辨率带来一些问题:
- 您的路径被划分在不同的模块中,但在路由定义中,您告诉路由器直接加载一个组件,而不是整个子模块(包含子模块路由定义)
- 在子模块中,您需要将第一条路径设置为空以加载模块主组件(在nest-1模块中,您的空路径将加载nest-1组件。加载子模块入口组件的作业不是父模块)
- 您的路由在路由器模块和主模块中都进行了设置(例如:在nest-a.module和nest-a.routing.module中,您有这一行
)。它们只能在一个位置定义(路由模块,然后将其导入主模块)RouterModule.forChild(nestARoutes),
- 路由器负责加载子模块(使用路径定义中的
属性),因此您不需要在父模块中导入子模块(es:nest-a.module不需要导入nest-1.module)loadChildren
- 在父模块路由定义中,需要使用
属性加载子模块(及其路由定义)loadChildren
- 在子模块中,第一条路径为空,并加载子模块入口组件(es:nest-1模块将加载nest-1组件)
希望我足够清楚。在更新代码Luca Regazzi的答案后,我开始在生产中收到以下错误消息 错误:未捕获(承诺中):错误:未加载运行时编译器 错误:未加载运行时编译器 似乎有了AOT,甚至还有另一种加载子对象的方法 而不是:
import { MyComponent } from './path/mycomponent.component';
...
loadChildren: () => MyComponent
应该是:
// no import
...
loadChildren: () => import('./path/mycomponent.component')
.then(m => m.MyComponent)
抱歉,我没有看到这似乎与路由相关,我还没有弄明白,顺便说一句,即使您将整个路径设置为
/nest-a/nest-1/test-z
未找到路由,您可以将{enableTracing:true}
作为.forRoot()的第二个参数传递给
方法来帮助您调试问题。路由器将在控制台中打印一些有用的信息。@Lucaregazi谢谢,我不知道它的存在。但是它更像是跟踪而不是调试。如果找到路由,它只记录有用的信息,否则不会真正记录任何信息。:(我想我知道问题出在哪里,但我无法解决问题,因为我保留了所有重定向..angular router希望在与规则匹配的同时完全使用url。问题是空路径匹配所有3个重定向条件(重定向到nest-a的路径、重定向到nest-1的路径和重定向到test-z的路径)。所有这些重定向可能会混淆路由器,因为许多文章都没有谈论loadChildren
…我正在迁移大量的代码,我们将看看它是如何运行的:)非常感谢。哇,看起来好多了。谢谢。