gpt4 book ai didi

Angular 2 延迟加载 : RangeError: Maximum call stack size exceeded

转载 作者:太空狗 更新时间:2023-10-29 17:50:03 24 4
gpt4 key购买 nike

我是 Angular 2 的新手,正在尝试延迟加载。我收到错误消息“RangeError:超出最大调用堆栈大小”

我尝试了许多其他类似的问题,但对我没有任何帮助。以下是我的代码供您引用。让我知道这个问题。

// App Module

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import { AppComponent } from './app.component';
import { ROUTER_CONFIG } from './app.routing';

@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
FormsModule,
HttpModule,
ROUTER_CONFIG
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }


// App Routing

import { RouterModule, Routes } from '@angular/router';
import { AppComponent } from './app.component';

const ROUTER_DATA: Routes = [
{ path:'home', component: AppComponent },
{ path:'lazy', loadChildren: './lazy/lazy.module#LazyModule' },
{ path:'', redirectTo:'home', pathMatch: 'full'}
];

export const ROUTER_CONFIG = RouterModule.forRoot(ROUTER_DATA);

//App Component

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

@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'app works!';
}


// Lazy module


import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { LazyComponent } from './lazy.component';
import { ROUTER_CONFIG } from './lazy.routing';

@NgModule({
declarations: [
LazyComponent
],
imports: [
CommonModule
],
providers: [],
bootstrap: [LazyComponent]
})
export class LazyModule { }


// lazy Routing

import { RouterModule, Routes } from '@angular/router';
import { LazyComponent } from './lazy.component';

const ROUTER_DATA: Routes = [
{ path:'', component: LazyComponent }
];

export const ROUTER_CONFIG = RouterModule.forRoot(ROUTER_DATA);

// lazy component

import { Component, OnInit } from '@angular/core';

@Component({
selector: 'app-lazy',
templateUrl: './lazy.component.html',
styleUrls: ['./lazy.component.css']
})
export class LazyComponent implements OnInit {

constructor() { }

ngOnInit() {
}

}
<!-- App HTML -->

<div [routerLink]="['/home']">Home</div>
<div [routerLink]="['/lazy']">Lazy</div>
<router-outlet></router-outlet>

<!-- lazy html -->

<p>
lazy works!
</p>

最佳答案

你忘了配置子路由

lazy.routing.ts

export const ROUTER_CONFIG = RouterModule.forRoot(ROUTER_DATA);

export const ROUTER_CONFIG = RouterModule.forChild(ROUTER_DATA);

lazy.module.ts

import { ROUTER_CONFIG } from './lazy.routing';
...

imports: [
ROUTER_CONFIG
]
})
export class LazyModule { }

关于 Angular 2 延迟加载 : RangeError: Maximum call stack size exceeded,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43843844/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com