RangeError:超出最大调用堆栈大小Lazy routing Angular 2

Vel*_*dan 41 typescript angular2-routing angular

我正在尝试将懒惰路由应用到我的应用程序中.

我有一个非常大的项目,当它在路由器已弃用时,我使用了AsyncRoute,但现在它已被删除.

所以我尝试实现最新的延迟加载,但是我遇到了一个问题 RangeError:超出了最大调用堆栈大小 我做错了什么?我在指令中做了所有代码.

请看一下

EncountersModule

    import { NgModule } from '@angular/core';
    // import { CommonModule } from '@angular/common';
    /* ---------------  !System modules  --------------- */

    import { SharedModule } from 'sharedModule';   //There is  a lot of shared components/directives/pipes (over 60) and it re-exports CommonModule so I can't avoid it
    /* ---------------  !App outer modules  --------------- */


    import { EncountersComponent } from './encounters.component';
    // import { PassCodeComponent } from '../../shared/components/passcode/passcode.component';


    @NgModule({
      imports: [ SharedModule ],
      declarations: [ EncountersComponent],
      exports: [ EncountersComponent ],
    })


    export class EncountersModule {  }
Run Code Online (Sandbox Code Playgroud)

这是我的app.routing.module

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


import { ImagingComponent }      from '../modules/index';
import { DashboardComponent }      from '../modules/index';
import { PrescriptionNoticesComponent }      from '../modules/index';
// import { EncountersComponent } from "../modules/encounters/encounters.component";
import { ScheduleComponent } from "../modules/schedule/schedule.component";
import { AdminComponent } from '../modules/index';




@NgModule({
  imports: [
    RouterModule.forRoot([
      {
        path: '',
        component: DashboardComponent,
        data: { label: 'Dashboard' }
      },
      {
        path: 'encounters',
        // component: EncountersComponent,
        loadChildren: 'production/modules/encounters/encounters.module#EncountersModule',
        data: { label: 'Encounters' }
      },
      {
        path: 'admin',
        component: AdminComponent,
        data: { label: 'Admin' }
      }
    ])
  ],
  exports: [
    RouterModule
  ]
})
export class AppRoutingModule {}




// const appRoutes: Routes = [
//   {
//     path: 'imaging',
//     component: ImagingComponent,
//     data: { label: 'Imaging' }
//   },
//   {
//     path: '',
//     component: DashboardComponent,
//     data: { label: 'Dashboard' }
//   },
//   {
//     path: 'prescription_notices',
//     component: PrescriptionNoticesComponent,
//     data: { label: 'Prescription Notices' }
//   },
//   {
//     path: 'encounters',
//     component: EncountersComponent,
//     data: { label: 'Encounters' }
//   },
//   {
//     path: 'schedule',
//     component: ScheduleComponent,
//     data: { label: 'Schedule' }
//   },
//   {
//     path: 'admin',
//     component: AdminComponent,
//     data: { label: 'Admin' }
//   }
// ];
//
// export const appRoutingProviders: any[] = [
//
// ];
//
// export const routing: ModuleWithProviders = RouterModule.forRoot(appRoutes);
Run Code Online (Sandbox Code Playgroud)

Mar*_*ski 80

loadChildren需要引用带路由的模块

通过为路由中的loadChildren属性赋值,您必须引用一个已实现路由系统的模块.换句话说,只引用一个导入RoutingModule并使用forChild(routes)方法配置它的模块.

import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
// import { CommonModule } from '@angular/common';
/* ---------------  !System modules  --------------- */

import { SharedModule } from 'sharedModule';   //There is  a lot of shared components/directives/pipes (over 60) and it re-exports CommonModule so I can't avoid it
/* ---------------  !App outer modules  --------------- */


import { EncountersComponent } from './encounters.component';
// import { PassCodeComponent } from '../../shared/components/passcode/passcode.component';

export const encountersModuleRoutes: Routes = [
  /* configure routes here */
];


@NgModule({
  imports: [ SharedModule, RouterModule.forChild(encountersModuleRoutes) ],
  declarations: [ EncountersComponent],
  exports: [ EncountersComponent ],
})


export class EncountersModule {  }
Run Code Online (Sandbox Code Playgroud)

  • 上帝祝福你.祝我的朋友过上幸福的生活.我试图从1小时差不多找到错误,你保存了我的'n`小时数:D (7认同)

Rea*_*ren 14

我不确定,因为它没有在文档(2.4.2)中明确提到,但是从"Angular Modules"和"Routing&Navigation"指南中的示例中,我引出了以下模式:

  • 惰性模块应该有自己的路由模块.
  • "lazy-routing.module"中定义的routes数组应该只有一个元素; 该path元素的属性应为空字符串; 的component(当懒惰模块,以便提供任何服务注入效果很好必要)和所引用的组件的模板应具有与一个元素属性应该定义<router-outlet>指令.这条路线通常有一个children属性.
  • path"app-routing.module"(在我的示例中为"lazyModulePrefix")中定义的惰性路由的属性值将是".lazy-routing.module"中定义的所有路径的前缀.

例如:

///////////// 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 {}
Run Code Online (Sandbox Code Playgroud)

.

///////////// 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 `<router-outlet>` 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 { }
Run Code Online (Sandbox Code Playgroud)

.

//////////////////// 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 { }
Run Code Online (Sandbox Code Playgroud)

.

//////////////// lazy-module-root.component.ts //////////////////
import { Component } from '@angular/core';

@Component({
    template: '<router-outlet></router-outlet>'
})
export class LazyModueRootComponent { }
Run Code Online (Sandbox Code Playgroud)

使用上面的代码,路由映射将是:

http:// host/login - > LoginComponent

http:// host/lazyModulePrefix - > LazyModuleHomeComponent

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

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

http:// host/anythingElse - > PageNotFoundComponent


Kur*_*gor 8

长话短说:当没有插入(忘记)子路由到功能模块时,我遇到了这个错误:

登陆.routing.ts export const LandingsRoutes = RouterModule.forChild(routes);

登陆模块.ts

imports: [
    CommonModule,
    // LandingsRoutes // <-- this I forgot and receive error Callstack exceeded
  ]
Run Code Online (Sandbox Code Playgroud)

如果您不将路由模块导出为模块 - 您需要导出唯一带有路由的路由器。只有遗​​忘的东西才会导致错误


小智 1

尝试删除评论。当我将我正在开发的应用程序中的路由器更新为当前版本时,我评论了旧路由器中的一堆内容,因为我不想丢失它。删除评论后,一些奇怪的错误就消失了。