覆盖组件,如服务?

Mil*_*lad 7 angular2-providers angular

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

 providers:[],
 imports : [MaterialInput]
Run Code Online (Sandbox Code Playgroud)

在里面MaterialInput,有一个名为MaterialInputComponent的组件

出于某些原因,我想用自己的方法覆盖该组件

所以我希望能够说:

 providers:[
    {
       provide: MaterialInputComponent,
       useClass : MyOwnInputComponent
    }
 ],
 imports : [MaterialInputModule]
Run Code Online (Sandbox Code Playgroud)

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

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

喜欢 :

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

  @Component({})
  export class OriginalMaterialInputComonent{
        greet(){ alert('Say Aloo'); }
  }
Run Code Online (Sandbox Code Playgroud)

我有一个类似的类:

  @Component({})
  export class OverrideMaterialInputComonent{

        greet(){ alert('Say yes we can'); } // overriden function
  }
Run Code Online (Sandbox Code Playgroud)

并且,假设我正在导入空洞MaterialInputModule

declarations:[
   {
    provide: OriginalMaterialInputComonent,
    useClass : OverrideMaterialInputComonent
  }
],
  imports : [MaterialInputModule]
Run Code Online (Sandbox Code Playgroud)

那可行吗?

moh*_*our -1

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

Overriding components
Run Code Online (Sandbox Code Playgroud)

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