我有一个带有模块(InformationPagesModule模块)的Simple Angular 2 App,该模块包含两个惰性加载组件(Info1组件和Info2组件)。在浏览器中输入以下路线时,我想加载该组件:
http://localhost:4200/info1-加载Info1Component
http://localhost:4200/info2-加载Info2Component
这里是兴趣类别:
app.component.html
<router-outlet></router-outlet>
app.module.ts
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { HomeComponent } from './home/home.component';
import { InformationPagesModule } from './information-pages/information-pages.module';
@NgModule({
declarations: [
AppComponent,
HomeComponent
],
imports: [
BrowserModule,
AppRoutingModule,
SharedModule,
InformationPagesModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
app-routing.module.ts
import { NgModule } from '@angular/core';
import { Routes, RouterModule, Router } from '@angular/router';
import { HomeComponent } from './home/home.component';
const routes: Routes = [
{path:'', component: HomeComponent},
{path:'info1', loadChildren: './information-pages/information-pages.module#InformationPagesModule'},
{pathMatch:'full', path:'info2', loadChildren: './information-pages/information-pages.module#InformationPagesModule'},
{ path: '**', redirectTo: '' }
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule],
})
export class AppRoutingModule { }
information-pages.module.ts
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { InformationPagesRoutingModule } from './information-pages-routing.module';
import { Info1Component } from './info1/info1.component';
import { Info2Component } from './info2/info2.component';
@NgModule({
imports: [
CommonModule,
InformationPagesRoutingModule
],
declarations: [Info1Component, Info2Component]
})
export class InformationPagesModule { }
information-pages-routing.module.ts
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { Info1Component } from './info1/info1.component';
import { Info2Component } from './info2/info2.component';
const routes: Routes = [
{path:'info1', component: Info1Component},
{path:'info2', component: Info2Component}
];
@NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule]
})
export class InformationPagesRoutingModule { }
我的问题是,为什么在输入某些路线/ info1或/ info2时显示空白页面?
我应该如何在我的应用程序中配置路由以使其起作用?
非常感谢!
在您的app-routing.module.ts中,您想声明一个父路由到一个延迟加载模块的所有路由。这意味着您可能希望将以/ info开头的所有路由映射到information-pages-routing.module.ts。在惰性加载的模块中声明的组件(info1和info2)的路由。
app-routing.module.ts
import { NgModule } from '@angular/core';
import { Routes, RouterModule, Router } from '@angular/router';
import { HomeComponent } from './home/home.component';
const routes: Routes = [
{ path:'', component: HomeComponent },
{ path: 'info', loadChildren: './information-pages/information-pages.module#InformationPagesModule' },
{ path: '**', redirectTo: '' }
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule],
})
export class AppRoutingModule { }
文件information-pages-routing.module.ts保持不变
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { Info1Component } from './info1/info1.component';
import { Info2Component } from './info2/info2.component';
const routes: Routes = [
{path:'info1', component: Info1Component},
{path:'info2', component: Info2Component}
];
@NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule]
})
export class InformationPagesRoutingModule { }
您现在可以通过路由/ info / info1访问Info1Component,并通过/ info / info2访问Info2Component。如果希望通过路由/ info1和/ info2访问组件,则可以创建两个延迟加载的模块,也可以将/ info1重定向到/ info / info1] >
您也可以考虑像这样加载子模块:
{ path: 'info', loadChildren: () => import('./information-pages/information-pages.module').then(m => m.InformationPageModule) }
而不是
{ path: 'info', loadChildren: './information-pages/information-pages.module#InformationPagesModule' }
有关更多信息,请参阅官方文档https://angular.io/guide/lazy-loading-ngmodules
使用这种方式而不是使用模块级路由,请使用AppRoutingModule来路由您的组件。