官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 葫芦岛市| 溧水县| 绥棱县| 明溪县| 临沂市| 浪卡子县| 黔江区| 临桂县| 安图县| 永济市| 西乡县| 平果县| 嵊州市| 比如县| 达拉特旗| 兰坪| 浮山县| 正宁县| 东阿县| 修文县| 南平市| 尼勒克县| 济阳县| 简阳市| 延长县| 长子县| 凤凰县| 通化县| 辉县市| 承德市| 河南省| 资源县| 德庆县| 龙州县| 封开县| 重庆市| 钦州市| 常宁市| 遵义市| 红原县| 胶南市|