我正在使用Angular 6.1,并尝试使用惰性路由设置应用程序模块和功能模块(称为实体模块)。我想创建一条路由 / entities / list ,链接到我的实体模块中的一个组件。但是我无法走这条路。相反,当我导航到 / list 时,将显示该组件。为什么我的路由忽略了父“ /实体”路径?
app.module.ts:
import {NgModule} from '@angular/core';
import {AppComponent} from './app.component';
import {AppRoutingModule} from './app-routing.module';
import {BrowserModule} from '@angular/platform-browser';
import {EntitiesModule} from './entities/entities.module';
@NgModule({
bootstrap: [AppComponent],
declarations: [AppComponent],
imports: [AppRoutingModule, BrowserModule, EntitiesModule],
providers: []
})
export class AppModule {}
app-routing.module.ts:
import {NgModule} from '@angular/core';
import {RouterModule, Routes} from '@angular/router';
const routes: Routes = [
{path: 'entities', loadChildren: './entities/entities.module#EntitiesModule'},
{path: '', redirectTo: '', pathMatch: 'full'}
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule {}
app.component.ts:
import {Component} from '@angular/core';
@Component({
selector: 'app-root',
template: 'app <router-outlet></router-outlet>'
})
export class AppComponent {}
entities / entities.module.ts:
import {NgModule} from '@angular/core';
import {EntitiesRoutingModule} from './entities-routing.module';
import {EntitiesComponent} from './entities.component';
@NgModule({
declarations: [EntitiesComponent],
imports: [EntitiesRoutingModule]
})
export class EntitiesModule {}
entities / entities-routing.module.ts:
import {NgModule} from '@angular/core';
import {Routes, RouterModule} from '@angular/router';
import {EntitiesComponent} from './entities.component';
const routes: Routes = [
{path: 'list', component: EntitiesComponent}
];
@NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule]
})
export class EntitiesRoutingModule {}
entities / entities.component.ts:
import {Component} from '@angular/core';
@Component({
selector: 'app-entities',
template: 'entities'
})
export class EntitiesComponent {}
我还尝试使用以下方法在实体模块中定义路线,但是该组件仍将显示为导航到 / list ,而不是 / entities / list 。
const routes: Routes = [
{
path: '', children: [
{path: 'list', component: EntitiesComponent}
]
}
];
我在做什么错?任何帮助表示赞赏!
答案 0 :(得分:1)
尝试将延迟加载的模块作为子项加载。
{
path: 'entities',
children: [{
path: '',
loadChildren: './entities/entities.module#EntitiesModule'
}]
}
答案 1 :(得分:1)
您正在尝试进行延迟加载。但是,您正在急切加载。
在app.module.ts
行中:
导入:[AppRoutingModule,BrowserModule,EntitiesModule
]
正在加载“实体”模块。从那里删除它以进行延迟加载,它应该可以工作。
答案 2 :(得分:0)
由于您正在懒惰地加载模块,但是没有初始化Entities组件。您的/ entities / list应该是实体组件的子代
您的entity / entities-routing.module.ts应该像
const route: Routes = [
{ path: '', component: EntitiesComponent, children: [
{ path: 'list', component: EntitiesListComponent }, // here should be your /entities/list path which i think you haven't made any
{ path: 'xyz', component: XYZComponent }, // entities/xyz and so on...
{ path: '', redirectTo: 'list', pathMatch: 'full'}
]}
];