在 Angular2 RC4 中,如何将组件添加到预编译数组?

In Angular2 RC4 how do I add components to the precompile array?

我刚刚将我的 Angular2 项目更新为 RC4,当我打开我的应用程序时,路由器现在在控制台中发出这条警告消息:

router.umd.js:2466 'FrontpageComponent' not found in precompile array.  To ensure all components referred to by the RouterConfig are compiled, you must add 'FrontpageComponent' to the 'precompile' array of your application component. This will be required in a future release of the router.

我试图弄清楚我到底需要做什么来解决这个问题,但由于文档很少,我找不到答案。这个预编译数组是什么,在哪里可以找到它或如何添加它?

在较新的路由器版本中,这不再是必需的。

<= RC.4

它只是 @Component()@Directive() 装饰器的附加参数:

@Component({
  selector: '...',
  template: '...',
  directives: [FrontpageComponent],
  precompile: [FrontpageCmponent]
})

https://github.com/angular/angular/blob/6c5b653593eff19c5b9342b2cf0195aca49379cb/modules/%40angular/core/src/metadata/directives.ts#L968

/**
* Defines the components that should be precompiled as well when
* this component is defined. For each components listed here,
* Angular will create a {@link ComponentFactory ComponentFactory} and store it in the
* {@link ComponentFactoryResolver ComponentFactoryResolver}.

据我观察,如果我在路由配置中使用 'redirectTo' 定义了组件,那么该组件也必须在根应用程序中定义 'precompile'。

不需要在指令中定义。使用下面的代码

    @Component({
  selector: '...',
  template: '...',
  directives: [],
  precompile: [FrontpageCmponent]
})

您必须将您的组件添加到应用程序组件元数据的 预编译数组中 才能删除该消息。

@Component({
selector:'my-app',    
template:`YOUR HTML
    <router-outlet></router-outlet>`
,styleUrls:['app/app.component.css']
,directives:[ROUTER_DIRECTIVES]
,providers:[YOURPROVIDERS]
,precompile:[YOURCOMPONENT]})
export class AppComponent{}

如果您将 "@angular/router": "3.0.0-beta.1" 升级到 "@angular/router": "3.0.0-beta.2" 。那么警告将待解决