angular - typescript - Angular延迟加载


import { NgModule } from '@angular/core';


import { Routes, RouterModule } from '@angular/router';



const routes: Routes = [


 { 


 path: 'home', 


 loadChildren: './shared/modules/homepage/homepage.module#HomepageModule' 


 },


];



@NgModule({


 imports: [RouterModule.forRoot(routes)],


 exports: [RouterModule]


})


export class AppRoutingModule { }

然后,确保使用延迟加载模块正确设置所有内容,在本例中为homepage模块,


import { NgModule } from '@angular/core';


import { CommonModule } from '@angular/common';


import { Routes, RouterModule } from '@angular/router';



//~~~~ Important:


import { HomeMainComponent } from './components/home-main/home-main.component';



const homeRoutes: Routes = [


 { 


 path: '',


 component: HomeMainComponent, 


 }


]



@NgModule({


 declarations: [


 HomeMainComponent


 ],


 imports: [


 CommonModule,


 RouterModule.forChild(homeRoutes),


 ],


 exports: [


 HomeMainComponent 


 ]


})


export class HomepageModule { }

谁知道我忽略了什么? 谢谢!

时间:

在新版本中延迟加载的方式已更改,请重构路由的配置:


const routes: Routes = [


 { 


 path: 'home', 


 loadChildren: () => import('./shared/modules/homepage/homepage.module').then(m => m.HomepageModule)


 },


];



在RouterModule中导出HomeModule :


@NgModule({


 declarations: [


 HomeMainComponent


 ],


 imports: [


 CommonModule,


 RouterModule.forChild(homeRoutes),


 ],


 exports: [


 HomeMainComponent,


 RouterModule 


 ]


})


export class HomepageModule { }



...