- Architecting Angular Applications with Redux,RxJS,and NgRx
- Christoffer Noring
- 163字
- 2021-08-27 19:56:16
Overriding an existing construct
There are cases when you want to override the default resolution of your construct. You can do so at the module level, but also at the component level. What you do is simply express which construct you are overriding and with which other construct. It looks like this:
@Component({
providers: [
{ provide: Service, useClass : FakeService }
]
})
The provide is our known construct and useClass is what it should point to instead. Let's imagine we implemented our Service like so:
export class Service {
no: number = 0;
constructor() {}
}
And we added the following override to a component:
@Component({
providers: [{ provide : Service, useClass: FakeService }]
})
The FakeService class has the following implementation:
export class FakeService {
set no(value) {
// do nothing
}
get no() {
return 99;
}
}
Now the component and all its view child components will always get FakeService when asking for the Service construct.
推薦閱讀
- 智慧城市:大數據、互聯網時代的城市治理(第4版)
- 物聯網識別技術
- Learning QGIS 2.0
- Web Application Development with R Using Shiny
- JBoss EAP6 High Availability
- 面向云平臺的物聯網多源異構信息融合方法
- PLC、現場總線及工業網絡實用技術速成
- React:Cross-Platform Application Development with React Native
- 世界互聯網發展報告·2019
- Yii Application Development Cookbook(Second Edition)
- Learning Swift(Second Edition)
- 從實踐中學習手機抓包與數據分析
- Hands-On Microservices with Node.js
- 5G時代的大數據技術架構和關鍵技術詳解
- 計算機網絡技術