Angular 2 使用 angular 路由器在 NPM 模块中延迟加载 NgModule
Angular 2 Lazy loading a NgModule in a NPM module with angular router
我一直在路由中延迟加载模块,例如
export const HomeRoute: Route = {
path: '',
component: HomeComponent,
canActivate: [AuthGuard],
children: [
{path: 'dashboard', loadChildren: 'app/+dashboard/db.module#DashboardModule'}
]
};
我想将我的 "pages" 放入 NPM 模块中。我应该在 loadChildren 属性中使用到 node_module 的路径是什么?我正在使用 angular-cli 1.0.0-beta.16
我试过了
{path: 'lazy', loadChildren: '../node_modules/hello-world/components#HelloWorld' }
还有
{path: 'lazy', loadChildren: 'hello-world/components#HelloWorld' }
导出的class为:-
import {Component} from '@angular/core';
@Component({
selector: 'hello-world',
styles: [`
h1 {
color: blue;
}
`],
template: `<div>
<h1 (click)="onClick()">{{message}}</h1>
</div>`
})
export class HelloWorld {
message = "Click Me ...";
onClick() {
this.message = "Hello World!";
console.log(this.message);
}
}
还有什么我应该尝试的吗?
检查 HelloWorld 是导出的 class 而不是 'default export'。否则它不会工作。
这目前是不可能的 - 请在此处查看 AngularJS CLI 团队的回复:-
https://github.com/angular/angular-cli/issues/2601
"This is a very relevant question. I don't think we support it in the
CLI atm." (Currently version beta 17)
Datumgeek 在这里以不同的方式(在 CLI 之外)实现了从模块延迟加载:- https://github.com/datumgeek/a2dyn/blob/master/README.md#development-server
如果将来可以在 Angular CLI 中使用,我会更新答案
我一直在路由中延迟加载模块,例如
export const HomeRoute: Route = {
path: '',
component: HomeComponent,
canActivate: [AuthGuard],
children: [
{path: 'dashboard', loadChildren: 'app/+dashboard/db.module#DashboardModule'}
]
};
我想将我的 "pages" 放入 NPM 模块中。我应该在 loadChildren 属性中使用到 node_module 的路径是什么?我正在使用 angular-cli 1.0.0-beta.16
我试过了
{path: 'lazy', loadChildren: '../node_modules/hello-world/components#HelloWorld' }
还有
{path: 'lazy', loadChildren: 'hello-world/components#HelloWorld' }
导出的class为:-
import {Component} from '@angular/core';
@Component({
selector: 'hello-world',
styles: [`
h1 {
color: blue;
}
`],
template: `<div>
<h1 (click)="onClick()">{{message}}</h1>
</div>`
})
export class HelloWorld {
message = "Click Me ...";
onClick() {
this.message = "Hello World!";
console.log(this.message);
}
}
还有什么我应该尝试的吗?
检查 HelloWorld 是导出的 class 而不是 'default export'。否则它不会工作。
这目前是不可能的 - 请在此处查看 AngularJS CLI 团队的回复:-
https://github.com/angular/angular-cli/issues/2601
"This is a very relevant question. I don't think we support it in the CLI atm." (Currently version beta 17)
Datumgeek 在这里以不同的方式(在 CLI 之外)实现了从模块延迟加载:- https://github.com/datumgeek/a2dyn/blob/master/README.md#development-server
如果将来可以在 Angular CLI 中使用,我会更新答案