首页 > 其他 > 详细

[Angular 2] BYPASSING PROVIDERS IN ANGULAR 2

时间:2016-10-12 07:04:30      阅读:280      评论:0      收藏:0      [点我收藏+]

Artical --> BYPASSING PROVIDERS IN ANGULAR 2

 

Here trying to solve one problem:

技术分享

On the left hand side of tree, there are 4 green blocks and 1 blue block. 

Meaning that three green dataService will use ‘OtherProvider‘ which in an instance of DataService, but different from the root Provider, even it is also a instance of DataService.

 

Problem is how the blue one on the left can use the root provider instead of ‘OtherProvider‘.

 

Creating alias tokens with useExisting

@NgModule({
  ...
  providers: [
    DataService,
    { provide: ROOT_DATASERVICE, useExisting: DataService }
  ]
})
export class AppModule {}
import { OpaqueToken } from ‘@angular/core‘;

export const ROOT_DATASERVICE = new OpaqueToken(‘DataService‘);

We create a new provider call ‘RootDataServcie‘ and using ‘useExisting‘ key. And we use OpaqueToken to avoid naming conflicts.

 

In the component:

@Component()
export class SomeComponent {
  
  constructor(private contactService: RootDataService) {}
}

 

[Angular 2] BYPASSING PROVIDERS IN ANGULAR 2

原文:http://www.cnblogs.com/Answer1215/p/5951378.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!