简体   繁体   English

无法导航到角度6中的延迟加载路线

[英]Can't navigate to lazy loading route in angular 6

I have looked at the following but i still can't navigate to the lazy loaded route(s). 我看过以下内容,但仍然无法导航到延迟加载的路由。 I have removed canLoad and canActivate for testing purposes. 我已删除canLoad和canActivate进行测试。 In my case, my parent route needs to receive an id. 就我而言,我的父路线需要接收一个ID。

How can I navigate to lazy loaded module child routes? 如何导航到延迟加载的模块子路由?

https://www.concretepage.com/angular/angular-canload-guard-example https://www.concretepage.com/angular/angular-canload-guard-example

Please take a look at PROJECTS SUB ITEMS SECTION -- LAZY LOADED in app routing module. 请查看PROJECTS SUB ITEMS SECTION -- LAZY LOADED应用程序路由模块中的PROJECTS SUB ITEMS SECTION -- LAZY LOADED

App Module 应用模块

@NgModule({
  declarations: [
    AppComponent,
    FooterComponent,
    HeaderComponent
  ],
  imports: [
    BrowserModule,
    BrowserAnimationsModule,
    //RouterModule,
    AppMaterialModule, //angular material components
    AppRoutingModule, //all routes are specified here, sub and line components module is specified here which in turn uses its own routing module
    AppSharedModule, //shared modules
    AppComponentsModule, //this has the components declared
    NgIdleKeepaliveModule.forRoot()
  ],
  //Title is the service by angular, using it for putting in document titles, check app.component
  providers: [Title, DataService, AuthService, AuthGuard, UtilityEnumService, LocalStorageService, ErrorMessageService, GroupService, ProgressBarService ], 
  bootstrap: [AppComponent]
})
export class AppModule { }

App Routing Module 应用程序路由模块

const appRoutes: Routes = [
  { path: '', component: HomeComponent, pathMatch: "full", data: { title: 'Home' } }, 
  { path: 'home', component: HomeComponent, data: { title: 'Home' } },

  /****************   PROJECTS MAIN SECTION *********************/
  { path: 'projects', component: ProjectsComponent, canActivate: [AuthGuard], data: { title: 'Projects' } },
  { path: 'projects/add', component: ProjectEditComponent, canActivate: [AuthGuard], data: { title: 'Add Project' } }, //new project
  { path: 'projects/:projectId', component: ProjectDetailComponent, canActivate: [AuthGuard], data: { title: 'Project Detail' } }, //detail - keep it before below edit
  { path: 'projects/:projectId/edit', component: ProjectEditComponent, canActivate: [AuthGuard], data: { title: 'Edit Project' } },

  /****************   PROJECTS SUB ITEMS SECTION -- LAZY LOADED *********************/
  {
    /* for easy loading, moved the sub and line to app-routing-sub-line.module and used inside app-components-sub-line.module */
    path: 'projects/:projectId/sub', loadChildren:'./app-components-sub-line.module#AppComponentsSubAndLineModule'
  },

  /****************   default, keep it at the bottom *********************/
  { path: '**', component: PageNotFoundComponent, data: { title: 'Page Not Found' } }
];

const routingConfig: ExtraOptions = {
  paramsInheritanceStrategy: 'always',
  preloadingStrategy: PreloadAllModules
}

@NgModule({
  imports: [
    CommonModule,
    RouterModule.forRoot(appRoutes, routingConfig) 
  ],
  exports: [RouterModule],
  declarations: []
})
export class AppRoutingModule { }

App Routing Sub and Line -- These need to be lazy loaded 应用程序路由子和行-这些需要延迟加载

const appSubAndLineRoutes: Routes = [
  {
    //for lazy loading the parent route needs to be empty since we have placed this inside the app-routing.module
    //path: 'projects/:projectId/sub', component: ProjectSubComponent, canActivate: [AuthGuard], data: { title: 'Project Sub' },
    path: '', component: ProjectSubComponent, data: { title: 'Project Sub' },
    children: [
      //catch all
      { path: '', component: SelectTheItemComponent, data: { title: 'Project Sub' } },
      { path: 'add', component: ProjectSubEditComponent, data: { title: 'Add Sub' } }, //new sub project
      { path: ':subId', component: ProjectSubDetailComponent, data: { title: 'Sub View' } }, //View - keep it before below edit
      { path: ':subId/detail-list', component: ProjectSubDetailListComponent, data: { title: 'Sub Detail' } }, //Detail - keep it before below edit
      { path: ':subId/edit', component: ProjectSubEditComponent, data: { title: 'Edit Sub' } },
      { path: ':subId/config-edit', component: ProjectSubConfigComponent, data: { title: 'Edit Sub Config' } },
      { path: ':subId/line/add', component: ProjectSubLineEditComponent, data: { title: 'Add Sub Line' } },
      { path: ':subId/line/:lineId/edit', component: ProjectSubLineEditComponent, data: { title: 'Edit Sub Line' } },
      { path: ':subId/line/:lineId/config', component: ProjectSubLineConfigComponent, data: { title: 'Config Sub Line' } },
      { path: ':subId/line/:lineId/view', component: ProjectSubLineViewComponent, data: { title: 'View Sub Line' } }
    ]
  }
];

@NgModule({
  imports: [RouterModule.forChild(appSubAndLineRoutes)],
  exports: [RouterModule]
})

export class AppRoutingSubAndLineModule { }

App Component Sub and Sub Line declarations module App Component Sub和Sub Line声明模块

This has the import for the sub and line routing module. 这具有子和线路路由模块的导入。 After looking at the examples, i don't think that i need to import routing module anywhere else. 看完示例之后,我认为不需要在其他任何地方导入路由模块。

@NgModule({
  declarations: [
    ProjectSubComponent,
    ProjectSubListComponent,
    ProjectSubDetailComponent,
    ProjectSubEditComponent,
    ProjectSubConfigComponent,
    ProjectSubDetailListComponent,
    ProjectSubLineEditComponent,
    ProjectSubLineConfigComponent,
    ProjectSubLineViewComponent,
    ProjectSubLineHdrComponent
  ],
  imports: [
    CommonModule,
    FormsModule,
    ReactiveFormsModule,
    AppRoutingSubAndLineModule, //sub and line routing module
    AppSharedModule //shared module
  ],
  exports: [
  ]
})

export class AppComponentsSubAndLineModule { }

And finally navigating to the route 最后导航到路线

this.router.navigate(['projects', id, 'sub']);

What am i missing here guys? 我在这里想念什么? My route needs to get an ID. 我的路线需要获取ID。 When i import the routes and components separately inside the app module, i can navigate to these fine. 当我分别在应用模块中导入路线和组件时,我可以导航至这些。

The order of imports in app module matters. 应用模块中的导入顺序很重要。 After fixing the order, it is now working . 确定订单后,它现在可以工作了。

@NgModule({
  declarations: [
    AppComponent,
    FooterComponent,
    HeaderComponent
  ],
  imports: [
    BrowserModule,
    BrowserAnimationsModule,
    AppMaterialModule, //angular material components
    RouterModule,
    AppSharedModule, //shared modules
    AppComponentsModule, //this has the general components decalred that we havent put in shared or child modules
    AppRoutingModule, //all general routes are here. keep it last
    NgIdleKeepaliveModule.forRoot()
  ],
  //Title is the service by angular, using it for putting in document titles, check app.component
  providers: [Title, DataService, AuthService, AuthGuard, UtilityEnumService, LocalStorageService, ErrorMessageService, GroupService, ProgressBarService ], 
  bootstrap: [AppComponent]
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM