Angular2:如何覆盖组件模板? [英] Angular2: How to override components template?

查看:91
本文介绍了Angular2:如何覆盖组件模板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在考虑将angular 1.4应用程序迁移到angular 2,我想知道是否有可能像我们使用$ provide.decorator(例如

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?).

我正在寻找类似 TestComponentBuilder.overrideTemplate 的东西,但是用于非测试场景. Angular2是否具有类似的功能?

I am looking for something like TestComponentBuilder.overrideTemplate but for a non-testing scenario. Does Angular2 comes with something similar?

推荐答案

从stackoverflow

Check out this answer from stackoverflow Override/extend third-party component's template. 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()
  }
}

但是有缺点:

  1. 它仍将编译原始组件.
  2. 如果您使用的是此方法,请不要忘记导入任何 用于thirdPartyClass模板.
  3. 是否在第三方依赖于该功能的第三方类中更新了功能 在模板上,您需要手工进行更新.

  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.
  3. If the functionality is updated in the thirdPartyClass that depends upon the template, you'll need to update by hand.

订阅此 Github问题进行进一步的更新.

Subscribe to this Github Issue for further updates.

这篇关于Angular2:如何覆盖组件模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆