【问题标题】:Routing module stop working: Uncaught (in promise): Error: Cannot find 'xxxModule'路由模块停止工作:未捕获(承诺):错误:找不到'xxxModule'
【发布时间】:2018-06-13 14:52:40
【问题描述】:

我对路由模块有一个奇怪的问题。用于工作的路由模块。但是,在我将另一个项目路由模块添加到具有不同模块名称的不同文件夹中后,在尝试导航到第 1 步时,我在该项目上遇到错误。

错误:未捕获(承诺中):错误:在“./+step1/step1.module”中找不到“Step1Module”。

step1.module:

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

import { AbcComponent } from './abc.component';
import { StepGuardService } from './shared/step-guard/step-guard.service';

const routes: Routes = [
  {
    path: '', component: AbcComponent, children: [
      {
        path: '',
        redirectTo: 'step1',
        pathMatch: 'full'
      },
      {
        path: 'step1',
        loadChildren: './+step1/step1.module#Step1Module',
        data: {
          preload: true,
          stepNumberOnNavigator: 1
        },
        canActivate: [StepGuardService]
      },
      {
        path: 'step2',
        loadChildren: './+step2/step2.module#Step2Module',
        data: {
          preload: true,
          stepNumberOnNavigator: 2
        },
        canActivate: [StepGuardService]
      }
    ]
  }
];

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

要解决这个问题,我必须在

行上用完全相同的字母(基本上是复制和粘贴)重新键入模块名称“Step1Module”
loadChildren: './+step1/step1.module#Step1Module',

并保存文件。然后错误将消失,项目将运行。我不明白这里发生了什么。由于实际上没有任何变化,因此我也无法将任何内容推送到 git 以解决服务器端的问题。

任何帮助将不胜感激!

谢谢!

更新:step1 和 step2 的路由模块

step1-routing.module.ts:

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

import { Step1Component } from './step1.component';

const routes: Routes = [
  {
    path: '',
    component: Step1Component,
    data: {
      bannerTitle: 'xxx',
      pageTitle: 'xxx'
    }
  }
];

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

step2-routing.module.ts:

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

import { Step2Component } from './step2.component';
import { Step2aComponent } from '../+step2/step2a/step2a.component';
import { QuestionnaireDetailComponent } from './questionnaire-detail/questionnaire-detail.component';
import { EligibilityDeclineComponent } from './eligibility-decline/eligibility-decline.component';

const routes: Routes = [
  {
    path: '',
    component: Step2aComponent,
    data: {
      bannerTitle: 'xxx',
      pageTitle: 'xxx'
    }
  },
  {
    path: 'eligibility',
    component: Step2Component,
    data: {
      bannerTitle: 'xxx',
      pageTitle: 'xxx'
    }
  },
...
  }
];

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

【问题讨论】:

  • 我们可以看看您对模块 Step2Module 的路由定义吗?您确定您为此使用了 forChild 而不是 forRoot 吗?
  • @WilliamKLEIN 我刚才在我的帖子中添加了两个路由定义。是的,我确定我使用的是 Child。谢谢!
  • @JulieC.你的问题已经用我的回答解决了你的问题。请标记为答案。

标签: javascript angular angular4-router angular-module


【解决方案1】:

请将完整路径应用于您的模块。将此行更改为 step1.module 文件中的以下内容:

 children: [
    {
      path: 'dashboard',
      loadChildren: 'app/components/dashboard/dashboard.module#DashboardModule'
    }
   ]

这些只是示例数据,请替换您的数据。 试试这个,请告诉我。

注意:- 路径开始 app/path/to/module/ 就像这个完整路径。

如有疑问,请访问这些网站并注意 loadChildren

https://scotch.io/tutorials/lazy-loading-in-angular-v2

https://angular.io/guide/lazy-loading-ngmodules

我希望这能解决你的问题。

谢谢,

卡南穆图。

【讨论】:

  • 谢谢,完整路径有效。不幸的是,在团队讨论之后,我们决定不朝那个方向发展,因为我们更喜欢路由模块具有延迟加载模块的相对路径。我们通过重命名新添加的项目路由模块解决了这个问题。它看起来像 Angular CLI 中的一个错误,当有两个具有相同名称的模块时,即使它们位于完全不同的文件夹中,它也无法识别正确的模块。
  • 如果您提出的问题场景有效,请标记为答案并投票。
猜你喜欢
  • 2017-05-14
  • 2017-10-07
  • 1970-01-01
  • 2017-10-05
  • 1970-01-01
  • 2018-01-03
  • 2019-02-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多