我正在使用Angular 5,并且我想实现延迟加载,但是它会返回空白页,而不会出现任何错误。
app-routing-module.ts
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { LoginComponent } from './views/content/login/login.component';
import { ErrorComponent } from './views/content/error/error.component';
const routes: Routes = [{
path: '',
component: LoginComponent
},
{
path: 'operations',
loadChildren: './views/operations/operations.module#OperationsModule'
},
{
path: '404',
component: ErrorComponent
},
{
path: '**',
redirectTo: '/404'
}];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule {}
operations-routing-module.ts
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { OperationsComponent} from './operations.component';
const routes: Routes = [
{
path: '',
component: OperationsComponent
}
];
@NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule]
})
export class OperationsRoutingModule {
constructor() {
console.log('hola mundo');
}
}
operation.module.ts
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { OperationsRoutingModule } from './operations-routing';
import { OperationsComponent} from './operations.component';
@NgModule({
imports: [CommonModule, OperationsRoutingModule],
declarations: [OperationsComponent]
})
export class OperationsModule {}
如果我使用component而不是loadChildren,则会显示该页面。
更新
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 { NavModule } from './views/nav/nav.module';
import { ContentModule } from './views/content/content.module';
import { FooterModule } from './views/footer/footer.module';
import { OperationsModule } from './views/operations/operations.module';
@NgModule({
declarations: [AppComponent],
imports: [
BrowserModule,
AppRoutingModule,
NavModule,
ContentModule,
FooterModule,
OperationsModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
其余路由(LoginComponent和ErrorComponent)运行正常,问题是由延迟加载造成的。 LoginComponent不为空,只是指向该组件的链接为空,它像应用程序的宿主一样工作。