当前位置:网站首页>【Question】what‘s the scenario of aliasing a class interface
【Question】what‘s the scenario of aliasing a class interface
2022-06-10 11:08:00 【zsy_1991】
官网的Aliasing a class interface 理解起来比较困难,除了以下使用场景,是否还要其他使用场景,欢迎大家参与留言讨论!!!
其中一个使用场景:
其中一个使用场景是如Reference 章节第二篇文章,可以将组建class 作为injectable 直接注册如DI 系统,然后子组件可以访问到他的父组件和祖父组建等。
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
边栏推荐
- cocoslua在vs2013的调试方法
- 图文,文字预训练方式长期学习ing。
- Vite's public directory
- Initial experience of a one-stop solution for general target detection and recognition based on shengteng AI heterogeneous computing architecture cann
- SqlSessionFactory和SqlSession详解
- [how much do you know about management] apart from independent conflict, you can't do it
- What is the difference between NoClassDefFoundError and classnotfoundexception
- Mit6.824-lab2d-2022 (detailed explanation of log compression)
- 使用matlab生成正弦波、三角波、方波的COE文件
- 【BUUCTF】[Zer0pts2020]Can you guess it?
猜你喜欢

Implementation code of several recent good papers (with source code download)

【SignalR全套系列】之在.Net6中实现SignalR分组通信

Flink CDC + Hudi 海量数据入湖在顺丰的实践

Gan learning notes KL divergence, JS divergence, Wasserstein distance

Music retrieval system based on SSH

渡远户外冲刺深交所:年营收3.5亿 林锡臻家族色彩明显

音质出色的降噪旗舰,女毒必选,贝壳王子MO3体验

Flink CDC 在大健云仓的实践

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

SaaS management system for digital commerce cloud business service industry: to achieve efficient business collaboration and help enterprises improve their digital transformation
随机推荐
常用颜色RGB、灰度值、取色值、透明度。
Why is your next computer a computer? Explore different remote operations
[C language] pragma pack (1) and pragma pack ()
软件测试质量与保证大题
SqlSessionFactory和SqlSession详解
第 1 天 栈与队列(简单)
杰理之BLE timer 时钟源不能选 OSC 晶振【篇】
企评家分不同维度解析:湖南长城科技信息有限公司企业成长性
隐式转换导致的cpu负载近100%
第 2 天 链表(简单)
数商云家具行业供应商评估管理系统:提升家具企业核心竞争力,实现数字化协同管理
软件测试基础
jpa + MySQL list转String存入数据库
Flink CDC + Hudi 海量数据入湖在顺丰的实践
数商云商业服务业SaaS管理系统:实现高效业务协作,助力企业完善数字化转型之路
【BUUCTF】[Zer0pts2020]Can you guess it?
Jiachuang biology rushes to the Growth Enterprise Market: the actual controller with annual revenue of 130million was once a professor of Wuhan University
RT thread usage manual (updated on 2022-0516)
如何才能把团队给带解散。。。
Common colors: RGB, gray value, color extraction value, transparency.