本文介绍了带有路由的延迟加载模块时,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/中所述router.html

这部分内容在某些博客文章中缺失,例如

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抛出“无法找到'默认'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 05:22