本文介绍了RC.5 在延迟加载带有路由的模块时抛出“无法找到‘默认’"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图在 angular2 rc 中延迟加载模块.应用程序.该模块包含路由本身.该应用程序试图在单击它注册的路由时延迟加载模块,但立即抛出

tried to lazy load a module in an angular2 rc. app. This module contains routes itself. The app is trying to lazy load the module on clicking the route it is registered on but immediately throws

Error: Uncaught (in promise): Error: Cannot find 'default' in './app/test/test.module'

应用中的路由定义如下:

Routes in app are defined like:

export const routes: Routes = [
  { path: '', component: StartComponent },
  { path: 'route1', component: Route1Component },
  { path: 'test', loadChildren: './app/test/test.module'},
  { path: '**', component: StartComponent } //page not found
];

TestModule 包含以下路由:

The TestModule contains following routes:

export const TestRoutes: Routes = [
      { path: '', redirectTo: 'overview' },
      { path: 'overview', component: TestOverviewComponent },
      { path: 'moredata', component: MoreDataComponent }
];

我删除了 redirectTo 并将默认路由指向一个真正的组件,但运气不佳.还尝试定义像

I removed redirectTo and pointed the default route to a real component without luck. Also tried defining routes with children like

export const AdminRoutes: Routes = [
  {
    path: 'test', component: TestComponent,
    children: [
          { path: '', redirectTo: 'overview' },
          { path: 'overview', component: TestOverviewComponent },
          { path: 'moredata', component: MoreDataComponent }
    ]
  }
];

结果相同.

任何提示可能有什么问题?加载模块按预期工作.

Any hints what might be wrong? Loading the module eagerly works as intended.

问候

推荐答案

你必须将你的模块导出的类名添加到 loadChildren 字符串中,如

You have to add the exported class name of your module to the loadChildren string, like

{ path: 'test', loadChildren: './app/test/test.module'},

改为

{ path: 'test', loadChildren: './app/test/test.module#TestModule'},

如官方文档中所述https://angular.io/docs/ts/latest/guide/路由器.html

如果我们仔细观察 loadChildren 字符串,我们可以看到它映射直接到我们之前构建的危机中心.module文件走出我们的危机中心功能区.在我们使用的文件路径之后a # 表示我们的文件路径在哪里结束并告诉路由器名称我们的 CrisisCenter NgModule.如果我们查看我们的危机中心模块文件,我们可以看到它匹配我们导出的 NgModule 类的名称.

某些博客文章中缺少此部分,例如

this part is missing in some blog posts, e.g.

https://angularjs.blogspot.de/2016/08/angular-2-rc5-ngmodules-lazy-loading.html

这篇关于RC.5 在延迟加载带有路由的模块时抛出“无法找到‘默认’"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-25 03:13