当前位置:网站首页>【Question】what‘s the scenario of aliasing a class interface
【Question】what‘s the scenario of aliasing a class interface
2022-06-10 11:15:00 【zsy_ one thousand nine hundred and ninety-one】
Official website Aliasing a class interface It's difficult to understand , In addition to the following usage scenarios , Whether there are other usage scenarios , Welcome to participate in the message discussion !!!
One of the usage scenarios :
One of the usage scenarios is as follows Reference Chapter 2 article , You can build class As injectable Direct registration such as DI System , Then the child component can access its parent component, grandfather component, etc .
Aliasing a class interface
Generally, writing variations of the same parent alias provider uses forwardRef as follows.
# dependency-injection-in-action/src/app/parent-finder.component.ts
providers: [{
provide: Parent, useExisting: forwardRef(() => AlexComponent) }],
To streamline your code, extract that logic into a helper function using the provideParent() helper function.
# dependency-injection-in-action/src/app/parent-finder.component.ts
// Helper method to provide the current component instance in the name of a `parentType`.
export function provideParent
(component: any) {
return {
provide: Parent, useExisting: forwardRef(() => component) };
}
Now you can add a parent provider to your components that’s easier to read and understand.
# dependency-injection-in-action/src/app/parent-finder.component.ts
providers: [ provideParent(AliceComponent) ]
Aliasing multiple class interfaces
To alias multiple parent types, each with its own class interface token, configure provideParent() to accept more arguments.
Here’s a revised version that defaults to parent but also accepts an optional second parameter for a different parent class interface.
# dependency-injection-in-action/src/app/parent-finder.component.ts
// Helper method to provide the current component instance in the name of a `parentType`.
// The `parentType` defaults to `Parent` when omitting the second parameter.
export function provideParent
(component: any, parentType?: any) {
return {
provide: parentType || Parent, useExisting: forwardRef(() => component) };
}
Next, to use provideParent() with a different parent type, provide a second argument, here DifferentParent.
# dependency-injection-in-action/src/app/parent-finder.component.ts
providers: [ provideParent(BethComponent, DifferentParent) ]
Reference
边栏推荐
猜你喜欢

杰理之长按复位及高电平复位【篇】

MIT6.824-lab2D-2022(日志压缩详细讲解)

杰理之获取 BLE 广播包、profile 数据的接口【篇】

Efficiency tools: utools

杰理之BLE IO 口中断及翻转【篇】

flutter SocketIO示例

指令集与应敏科技达成战略合作,国产原创物联网操作系统助力电力检测「智慧化」...

Digital supply chain collaboration system for digital commerce cloud communication industry: empowering communication enterprises to improve supply business and enhance market competitiveness

In commemoration of the 16th day of the first month, the total number of visits to the studio exceeded one million

Detailed explanation of redis
随机推荐
香槐路的香槐花,匆匆四年无归期。
Manual topic 01
Review the growth evaluation of central enterprises and listed companies
flutter SocketIO示例
Dynamic programming (MID)
kubernetes 设置 Master 可调度与不可调度
[WIP] Openstack Masakari (by quqi99)
第 1 天 栈与队列(简单)
图文,文字预训练方式长期学习ing。
杰理之长按复位及高电平复位【篇】
【时间序列预测】ESRNN之论文阅读
Yutai semiconductor rushes to the scientific innovation board: the annual revenue is 830million, and the actual controller is American
软件测试基础
PV operation daily question - buffer problem (advanced version)
数商云家具行业供应商评估管理系统:提升家具企业核心竞争力,实现数字化协同管理
深度剖析「圈组」关系系统设计 | 「圈组」技术系列文章
golang CLI框架--cobra
中台:数据中台、业务中台、技术中台、应用中台、AI中台……
企评家分不同维度解析:湖南长城科技信息有限公司企业成长性
flutter websocket示例