Angular 2覆盖组件

时间:2016-11-22 05:40:25

标签: laravel angular override modular angular-template

我来自Laravel世界,对Angular 2世界有点新鲜,我很难弄明白:

是否可以覆盖Angular 2中的组件或模板,就像我们用来覆盖Laravel中供应商/自定义包的视图一样?

这是一个虚拟文件夹结构,可能表达我的意思:

|-resources
   |-assets
      |-typescript
         |-core
            |-core.component.ts    //overridded core component and template
            |-core.template.html
|-Modules
   |-Core
      |-Resources
         |-assets
            |-typescript
               |-core
                  |-core.component.ts  //main module component and template
                  |-core.template.html

core.template.html(原创)

<div>
    <p> This is a core component's template</p>
    <button>Click me! </button>
</div>

core.template.html(重写)

<div>
    <p> This is a overridden core component's template</p>
    <p> Removed the button and overridden with p-tag </p>
</div>

希望我已经清楚地说明了我面临的问题。

1 个答案:

答案 0 :(得分:0)

覆盖组件的teamplete直到Angular 2的未解决问题

来自:https://github.com/angular/angular/issues/11144

的信息

但是现在您可以使用Reflect来更改组件的元数据。

import {Component} from 'angular2/core'

@Component({
  selector: 'thirdpartycomp',
  providers: [],
  template: `Hello From Third Party App!`,
  directives: []
})
export class ThirdParty{}

annotations = Reflect.getMetadata('annotations', Thing);
for (let i = 0; i < annotations.length; i += 1) {
  if (annotations[i].constructor.name === 'ComponentMetadata') {
    annotations[i].template = 'Hello From My App!';
    break;
  }
}

@Component({
  selector: 'my-app',
  directives: [ThirdParty],
  template: `<thirdpartycomp></thirdpartycomp>`,
})
export class App {}