Override/extend third-party component's template

后端 未结 2 1074
清酒与你
清酒与你 2021-02-19 03:08

I am currently importing a third party component. For my use case I need to override that specific component template.

Since this is a third party component, and import

2条回答
  •  南旧
    南旧 (楼主)
    2021-02-19 03:48

    You can use Reflect to change metadata of component. Here's the super simple example:

    import {Component} from 'angular2/core'
    
    @Component({
      selector: 'thing',
      template: `Hi!`,
    })
    export class Thing {}
    
    annotations = Reflect.getMetadata('annotations', Thing);
    for (let i = 0; i < annotations.length; i += 1) {
      if (annotations[i].constructor.name === 'ComponentMetadata') {
        annotations[i].template = 'Ho!';
        break;
      }
    }
    
    @Component({
      selector: 'my-app',
      directives: [Thing],
      template: ``,
    })
    export class App {}
    

    Just make sure you update template before injecting it into parent component. Also check which metadata you need to access, might be DirectiveMetadata in your case.

提交回复
热议问题