覆盖组件,如服务?

时间:2017-04-03 04:25:47

标签: angular angular2-providers

假设我们导入一个Angular Material模块:

 providers:[],
 imports : [MaterialInput]

MaterialInput内,有一个名为:MaterialInputComponent

的组件

由于某些原因,我想用我自己的

覆盖该组件

所以我希望能够说:

 providers:[
    {
       provide: MaterialInputComponent,
       useClass : MyOwnInputComponent
    }
 ],
 imports : [MaterialInputModule]

我知道我们可以覆盖这样的服务,但是它也可以用于组件或指令吗?

更新 : 我不是在寻找组件继承,我想要的是使用类似Material Module的东西,但有时我想覆盖它的组件行为,就像你对服务一样。

喜欢:

如果这是MaterialInput组件背后的原始代码,它位于我的节点模块中。

  @Component({})
  export class OriginalMaterialInputComonent{
        greet(){ alert('Say Aloo'); }
  }

我有类似的课程:

  @Component({})
  export class OverrideMaterialInputComonent{

        greet(){ alert('Say yes we can'); } // overriden function
  }

而且,我说我导入了一个空洞MaterialInputModule

declarations:[
   {
    provide: OriginalMaterialInputComonent,
    useClass : OverrideMaterialInputComonent
  }
],
  imports : [MaterialInputModule]

可行吗?

1 个答案:

答案 0 :(得分:-1)

你可以使用装饰器,你可以查看下面的jsfiddle示例

Overriding components

https://jsfiddle.net/mohan_rathour/sqv0jx21/