简体   繁体   English

Angular2:如何覆盖组件模板?

[英]Angular2: How to override components template?

I am considering migrating an angular 1.4 application to angular 2 and I wonder if it will be possible to override components' template like we do in angular1 using $provide.decorator (like Can you override specific templates in AngularUI Bootstrap? ). 我正在考虑将angular 1.4应用程序迁移到angular 2,我想知道是否有可能像使用$ provide.decorator一样在angular1中覆盖组件的模板(例如您可以覆盖AngularUI Bootstrap中的特定模板吗? )。

I am looking for something like TestComponentBuilder.overrideTemplate but for a non-testing scenario. 我正在寻找类似TestComponentBuilder.overrideTemplate东西,但是用于非测试场景。 Does Angular2 comes with something similar? Angular2是否附带类似的东西?

Check out this answer from stackoverflow Override/extend third-party component's template . 从stackoverflow 覆盖/扩展第三方组件的模板中查看此答案。 The main idea is you can write your own component and extend third party component. 主要思想是您可以编写自己的组件并扩展第三方组件。

import {component} from 'angular2/core';
import {thirdPartyClass} from 'example/example';

@Component({
  selector: 'my-selector',
  template: '<div>my template</div>'
})

export class MyOwnComponent extends thirdPartyClass {
  constructor() {
     super()
  }
}

But there are downsides : 但是有缺点:

  1. It will still compile original component. 仍将编译原始组件。
  2. If you are using this method, don't forget to import any pipes that are used in the thirdPartyClass template. 如果使用此方法,请不要忘记导入thirdPartyClass模板中使用的所有管道。
  3. If the functionality is updated in the thirdPartyClass that depends upon the template, you'll need to update by hand. 如果在依赖模板的thirdPartyClass中更新了功能,则需要手动更新。

    Subscribe to this Github Issue for further updates. 订阅此Github问题以获取进一步更新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM