[Angular 2] Understanding OpaqueToken
When using provider string tokens, there’s a chance they collide with other third-party tokens. Angular has with the concept of opaque tokens, that allow us to make whatever token we use unique, so we don’t run into collision problems. In this lesson we’ll explore how they work.
providers: [
TodoService,
ConsoleService,
TranslateService,
,{
provide: LoggerProvider, useFactory: (cs, ts) => {
return new LoggerProvider(cs, ts, true)
},
deps: [ConsoleService, TranslateService]
}
,{
provide: apiUrl,
useValue: 'http://localhost:3000/api'
}
],
It turns out that this can be problematic in case we're using, for example, a third-party library that comes with its own provider that introduces the same token.
third-party.ts:
export const THIRD_PARTY_PRIVODERS = [
{
provide: apiUrl,
useValue: 'someurl'
}
]
So when you inject third-pary library into our app.ts:
providers: [
TodoService,
ConsoleService,
TranslateService,
,{
provide: LoggerProvider, useFactory: (cs, ts) => {
return new LoggerProvider(cs, ts, true)
},
deps: [ConsoleService, TranslateService]
}
,{
provide: apiUrl,
useValue: 'http://localhost:3000/api'
}
,THIRD_PARTY_PROVIDERS
],
Then it will rewrite our 'apiUrl' because THIRD_PARTY_PROVIDERS comes behind apiUrl.
To solve this problem, Angular introduce OpaqueToken.
app.tokens.ts:
import {OpaqueToken} from '@angular/core'; export const API_URL = new OpaqueToken('apiUrl')
Now 'API_URL' is a class instance not just a string, class instance is always unique.
Then in app.ts:
import {API_URL} from './app.tokens' providers: [
TodoService,
ConsoleService,
TranslateService,
,{
provide: LoggerProvider, useFactory: (cs, ts) => {
return new LoggerProvider(cs, ts, true)
},
deps: [ConsoleService, TranslateService]
}
,{
provide: API_URL,
useValue: 'http://localhost:3000/api'
}
,THIRD_PARTY_PROVIDERS
],
In DataService:
import {LoggerProvider} from './LoggerProvider';
import {Injectable} from '@angular/core';
import {Http} from '@angular/core';
import {Inject} from '@angular/core';
import {API_URL} from './app.tokens'; @Injectable
export class TodoService{ constructor(@Inject(API_URL) private apiUrl, private logger: LoggerProvider, private http: Http){ } getTodos(){
this.logger.debug('Items', this.todos);
return this.http.get(`${this.apiUrl}/todos`).map(res => res.json());
}
}
Now we won't have conflict anymore.
As a general rule, always create opaque tokens when using string tokens for providers. For example:
third-party.ts:
import {OpaqueToken} from '@angular/core'; const API_URL = new OpaqueToken('apiUrl');
const CONFIG_URL = new OpaqueToken('configUrl'); export const THIRD_PARTY_PROVIDERS = [
{
provide: API_URL,
useValue: 'somevalue'
},
{
provide: CONFIG_URL,
useValue: 'somevalue'
}
]
[Angular 2] Understanding OpaqueToken的更多相关文章
- [Angular 2] Understanding Pure & Impure pipe
First, how to use a build in pipe: <div class="pipe-example"> <label>Uppercase ...
- [Angular 2] Understanding @Injectable
In order to resolve a dependency, Angular’s DI uses type annotations. To make sure these types are p ...
- 来自 Thoughtram 的 Angular 2 系列资料
Angular 2 已经正式 Release 了,Thoughtram 已经发布了一系列的文档,对 Angular 2 的各个方面进行深入的阐释和说明. 我计划逐渐将这个系列翻译出来,以便对大家学习 ...
- angular default project (angular.json的解读)
Change the default Angular project Understanding it's purpose and limits Klaus KazlauskasFollow Nov ...
- Angular 4+ 修仙之路
Angular 4.x 快速入门 Angular 4 快速入门 涉及 Angular 简介.环境搭建.插件表达式.自定义组件.表单模块.Http 模块等 Angular 4 基础教程 涉及 Angul ...
- Angular复习笔记6-依赖注入
Angular复习笔记6-依赖注入 依赖注入(DependencyInjection)是Angular实现重要功能的一种设计模式.一个大型应用的开发通常会涉及很多组件和服务,这些组件和服务之间有着错综 ...
- angular2 学习笔记 ( DI 依赖注入 )
refer : http://blog.thoughtram.io/angular/2016/09/15/angular-2-final-is-out.html ( search Dependency ...
- AngularJs学习笔记--Understanding Angular Templates
原版地址:http://docs.angularjs.org/guide/dev_guide.mvc.understanding_model angular template是一个声明规范,与mode ...
- 【转】Understanding the Angular Boot Process
原文: https://medium.com/@coderonfleek/understanding-the-angular-boot-process-9a338b06248c ----------- ...
随机推荐
- C++ STL算法系列1---count函数
一.count函数 algorithm头文件定义了一个count的函数,其功能类似于find.这个函数使用一对迭代器和一个值做参数,返回这个值出现次数的统计结果. 编写程序读取一系列int型数据,并将 ...
- UVA 10529-Dumb Bones(概率dp)
题意: 给出放一个多米诺骨牌,向左向右倒的概率,求要放好n个骨牌,需要放置的骨牌的期望次数. 分析: 用到区间dp的思想,如果一个位置的左面右面骨牌都已放好,考虑,放中间的情况, dp[i]表示放好前 ...
- MDI端口和MDIX端口是什么? 又有什么作用?
是网线的标准A类接法和B类接法.也就是人们通常所说的交叉网线和直联网线.直联网线就是 白黄 黄 白绿 蓝 白兰 绿 白棕 棕 另一端同样如此.交叉网线就是 另一端的1和3,2和6对调.这样就成了交叉网 ...
- 一个鼠标键盘控制两台甚至多台主机的方法--Synergy
在多台主机,不同系统中操作.避免了更换键鼠的麻烦.即使下面图中的功能. 鼠标同时在三台或者多台主机之间进行移动,而且是无缝滑动,鼠标直接从左滑倒右,而且支持,这台电脑复制,另一台黏贴.非常的方便实用. ...
- 初识JAVA(【面向对象】:pub/fri/pro/pri、封装/继承/多态、接口/抽象类、静态方法和抽象方法;泛型、垃圾回收机制、反射和RTTI)
JAVA特点: 语法简单,学习容易 功能强大,适合各种应用开发:J2SE/J2ME/J2EE 面向对象,易扩展,易维护 容错机制好,在内存不够时仍能不崩溃.不死机 强大的网络应用功能 跨平台:JVM, ...
- Java Client for Google Cloud Storage
关于Google Cloud Storage Google Cloud Storage有益于大文件的存储与服务(serve).此外,Cloud Storage提供了对访问控制列表(ACLs)的使用,提 ...
- 树莓派使用8188eu无线网卡
#已经集成了8188eu驱动的镜像 http://cassidy.pi3g.com/rpi_images/raspbian-wifi-fix130523.7z #需要修改的信息 sudo nano / ...
- SCAU 1138 代码等式
1138 代码等式 时间限制:500MS 内存限制:65536K提交次数:59 通过次数:21 题型: 编程题 语言: 无限制 Description 一个代码等式就是形如x1x2...xi=y ...
- nova --debug image-list
nova --debug image-list DEBUG (session:) REQ: curl -g -i -X GET http://liberty-aio:35357/v3 -H " ...
- ES6学习(1)——如何通过babel将ES6转化成ES5
使用babel编译ES6 babel是一个工具,可以通过多个平台,让js文件从ES6转化成ES5,从而支持一些浏览器并未支持的语法. Insall babel $ sudo npm install b ...