码迷,mamicode.com
首页 > 其他好文 > 详细

[Angular 2] BYPASSING PROVIDERS IN ANGULAR 2

时间:2016-10-12 07:05:58      阅读:150      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!