【问题标题】:Angular lazy loading with parameters带参数的角延迟加载
【发布时间】:2018-08-06 12:25:43
【问题描述】:

我正在尝试通过阅读这篇文章来解决延迟加载问题:

https://www.concretepage.com/angular-2/angular-module-loading-eager-lazy-and-preloading

但它似乎对我不起作用。 我已经设置了这条路线(在我开始搞乱延迟加载之前),它看起来像这样:

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

import { StepsComponent } from './steps.component';

const stepRoutes: Routes = [
  { path: 'steps', redirectTo: 'steps/one', pathMatch: 'full' },
  { path: 'steps/:path', component: StepsComponent }
];

@NgModule({
  imports: [ RouterModule.forChild(stepRoutes) ],
  exports: [ RouterModule ]
})
export class StepsRoutingModule { }

所以我想让它延迟加载。所以起初我只是从 AppModule 中删除了我的 StepsModule 并将 AppRouting 模块更新为:

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

const routes: Routes = [
  { path: '', redirectTo: 'home', pathMatch: 'full' },

  // Lazy load
  //{ path: 'home', loadChildren: 'app/home/home.module#HomeModule '},
  { path: 'steps', loadChildren: 'app/steps/steps.module#StepsModule '}
]

@NgModule({
  imports: [ RouterModule.forRoot(routes) ],
  exports: [ RouterModule ]
})
export class AppRoutingModule { }

我将我的 StepsRouting 模块更改为:

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

import { StepsComponent } from './steps.component';

const stepRoutes: Routes = [
  { path: '', redirectTo: 'steps/one', pathMatch: 'full' },
  { path: 'steps/:path', component: StepsComponent }
];

@NgModule({
  imports: [ RouterModule.forChild(stepRoutes) ],
  exports: [ RouterModule ]
})
export class StepsRoutingModule { }

但它不起作用。 我可以看到,当我为应用程序提供服务时,StepsModule 没有加载,但是如果我尝试导航到 steps 视图,我没有收到任何错误,Url 发生了变化,但是它不加载 StepsModule

有人知道为什么吗?

【问题讨论】:

  • 你有什么错误?
  • 没有错误,如果我注释掉全部内容,我会得到:Uncaught (in promise): TypeError: undefined is not a function
  • Angular 的延迟加载并不像它应该的那样简单。我发现延迟加载的模块本身必须有路由,即使它要在其中加载单个组件。
  • 你在AppComponent 中添加了router-outlet 吗?
  • 是的,正如我所说,它工作正常,无需尝试延迟加载

标签: angular


【解决方案1】:

您现在拥有它的方式将使路线/steps/steps/:path。从stepRoutes 中删除steps/

{ path: ':path', component: StepsComponent }

【讨论】:

  • 这不起作用:(我仍然得到错误:Uncaught (in promise): TypeError: __webpack_require__.e is not a function
  • 该错误通常是您导入共享服务的位置等问题。通过将内容移入/移出我的应用程序模块和共享模块,我已经能够摆脱这些错误。我必须查看您的整个代码库才能为您提供更多帮助
猜你喜欢
  • 2020-02-03
  • 1970-01-01
  • 1970-01-01
  • 2018-05-22
  • 2023-04-11
  • 1970-01-01
  • 2017-12-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多