博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Angular 2] BYPASSING PROVIDERS IN ANGULAR 2
阅读量:5077 次
发布时间:2019-06-12

本文共 976 字,大约阅读时间需要 3 分钟。

Artical --> 

 

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) {}}

 

转载于:https://www.cnblogs.com/Answer1215/p/5951378.html

你可能感兴趣的文章
Android实现 ScrollView + ListView无滚动条滚动
查看>>
java学习笔记之String类
查看>>
UVA 11082 Matrix Decompressing 矩阵解压(最大流,经典)
查看>>
硬件笔记之Thinkpad T470P更换2K屏幕
查看>>
蓝桥杯-分小组-java
查看>>
Android Toast
查看>>
iOS开发UI篇—Quartz2D使用(绘制基本图形)
查看>>
docker固定IP地址重启不变
查看>>
桌面图标修复||桌面图标不正常
查看>>
JavaScript基础(四)关于对象及JSON
查看>>
JAVA面试常见问题之Redis篇
查看>>
jdk1.8 api 下载
查看>>
getElement的几中属性介绍
查看>>
HTML列表,表格与媒体元素
查看>>
雨林木风 GHOST_XP SP3 快速装机版YN12.08
查看>>
数据结构3——浅谈zkw线段树
查看>>
Introduction to my galaxy engine 2: Depth of field
查看>>
设计器 和后台代码的转换 快捷键
查看>>
STL容器之vector
查看>>
数据中心虚拟化技术
查看>>