RangeError: Maximum call stack size exceeded Lazy routing Angular 2

后端 未结 7 862
礼貌的吻别
礼貌的吻别 2020-12-13 23:38

I\'m trying to implement lazy routing into my app.

I have a very big project and when it was at router-deprecated I used AsyncRoute, but now it was removed.

7条回答
  •  猫巷女王i
    2020-12-14 00:20

    I am not sure because it is not explicitly mentioned in the documentation (2.4.2), but from the examples in the "Angular Modules" and "Routing & Navigation" guides, I have induced the following pattern:

    • The lazy module should have his own routing module.
    • The routes array defined in the "lazy-routing.module" should have a single element; the path property of that element should be an empty string; the component property should be defined (necessary when the lazy module provides any service in order to injection works well) and the template of the referenced component should have an element with the directive. This route usually has a children property.
    • The value of the path property of the lazy route defined in the "app-routing.module" ("lazyModulePrefix" in my example) would be the prefix of all the paths defined in the ".lazy-routing.module".

    For example:

    ///////////// app-routing.module.ts /////////////////////
    import { NgModule  } from '@angular/core';
    import { Routes, RouterModule } from '@angular/router';
    
    import { LoginComponent } from './login/login.component';
    import { PageNotFoundComponent } from './page-not-found.component';
    
    const appRoutes: Routes = [
      { path: 'login', component: LoginComponent },
      { path: 'lazyModulePrefix', loadChildren: 'app/lazyModulePath/lazy.module#LazyModule' }, // 
      { path: '', redirectTo: 'login', pathMatch: 'full'},
      { path: '**', component: PageNotFoundComponent },
    ];
    
    @NgModule({
      imports: [RouterModule.forRoot(appRoutes)],
      exports: [RouterModule]
    })
    export class AppRoutingModule {}
    

    .

    ///////////// lazy-routing.module.ts /////////////////////
    import { NgModule } from '@angular/core';
    import { Routes, RouterModule } from '@angular/router';
    
    import { LazyModuleRootComponent } from './lazy-module-root.component';
    import { LazyModuleHomeComponent } from './lazy-module-home.component';
    import { AComponentDeclaredInTheLazyModule1 } from './a-component-declared-in-the-lazy-module-1.component';
    import { AComponentDeclaredInTheLazyModule2 } from './a-component-declared-in-the-lazy-module-2.component';
    
    const lazyModuleRoutes: Routes = [ // IMPORTANT: this array should contain a single route element with an empty path. And optionally, as many children as desired.
        { path: '',
          component: LazyModuleRootComponent, // the `component` property is necessary when the lazy module provides some service in order to injection work well. If defined, the referenced component's template should have an element with the `` directive.
          children: [ 
            { path: '', component: LazyModuleHomeComponent }, // this component has no diference with the other children except for the shorter route.
            { path: 'somePath1', component: AComponentDeclaredInTheLazyModule1 },
            { path: 'somePath2', component: AComponentDeclaredInTheLazyModule2 },
        ] } 
    ];
    
    @NgModule({
        imports: [RouterModule.forChild(lazyModuleRoutes)],
        exports: [RouterModule]
    })
    export class LazyRoutingModule { }
    

    .

    //////////////////// lazy.module.ts ////////////////////
    import { NgModule } from '@angular/core';
    import { CommonModule } from '@angular/common';
    
    import { SharedModule } from '../shared/shared.module';
    import { LazyRoutingModule } from './lazy-routing.module';
    import { LazyModuleRootComponent } from './lazy-module-root.component';
    import { LazyModuleHomeComponent } from './lazy-module-home.component';
    import { AComponentDeclaredInTheLazyModule1 } from './a-component-declared-in-the-lazy-module-1.component';
    import { AComponentDeclaredInTheLazyModule2 } from './a-component-declared-in-the-lazy-module-2.component';
    
    @NgModule({
        imports: [
            CommonModule,
            SharedModule,
            LazyRoutingModule,
        ],
        declarations: [
            LazyModuleRootComponent,
            LazyModuleHomeComponent,
            AComponentDeclaredInTheLazyModule1,
            AComponentDeclaredInTheLazyModule2,
        ]
    })
    export class LazyModule { }
    

    .

    //////////////// lazy-module-root.component.ts //////////////////
    import { Component } from '@angular/core';
    
    @Component({
        template: ''
    })
    export class LazyModueRootComponent { }
    

    With the above code, the routes mapping would be:

    http://host/login -> LoginComponent

    http://host/lazyModulePrefix -> LazyModuleHomeComponent

    http://host/lazyModulePrefix/somePath1 -> AComponentDeclaredInTheLazyModule1

    http://host/lazyModulePrefix/somePath2 -> AComponentDeclaredInTheLazyModule2

    http://host/anythingElse -> PageNotFoundComponent

提交回复
热议问题