[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 ----------- ...
随机推荐
- 省市区(县)三级联动代码(js 数据源)
ylbtech-JavaScript-Utility:省市区(县)三级联动代码(js 数据源) 省市区(县)三级联动代码(js 数据源) 1.A,源代码(Source Code)返回顶部 1.A.1, ...
- 通过ListActivity使用ListView布局方法
先简单的介绍一下ListActivity ListActivity是一个专门显示ListView的Activity类,它内置了ListView对象,只要我们设置了数据源,就会自动地显示出来.ListA ...
- 常见设计模式解析和实现(C++)FlyWeight模式
作用:运用共享技术有效地支持大量细粒度的对象 UML结构图: 解析: Flyweight模式在大量使用一些可以被共享的对象的时候使用.比如,在QQ聊天时很多时候你懒得回复又不得不回复,一般会用一些客套 ...
- mapreduce优化总结
集群的优化 1.合理分配map和reduce任务的数量(单个节点上map任务.reduce任务的最大数量) 2.其他配置 io.file.buffer.size hadoop访问文件的IO操作都需要通 ...
- 【和我一起学python吧】python的数据类型
python的元组.列表.字典数据类型是很python(there python is a adjective )的数据结构.这些结构都是经过足够优化后的,所以如果使用好的话,在某些area会有很大的 ...
- PHP的MySQL扩展:PHP访问MySQL的常用扩展函数
来源:http://www.ido321.com/1024.html 一.PHP连接数据库及基本操作 MySQL采用的是’客户机/服务器’架构.使用PHP安装的MySQL扩展函数,和直接使用客户端软件 ...
- aggregation(2):adaptive Boosting (AdaBoost)
给你这些水果图片,告诉你哪些是苹果.那么现在,让你总结一下哪些是苹果? 1)苹果都是圆的.我们发现,有些苹果不是圆的.有些水果是圆的但不是苹果, 2)其中到这些违反"苹果都是圆的" ...
- [Hive - LanguageManual] Statistics in Hive
Statistics in Hive Statistics in Hive Motivation Scope Table and Partition Statistics Column Statist ...
- PHP操作cookie函数:setcookie()与setrawcookie()
PHP setcookie() 函数向客户端发送一个 HTTP cookie.cookie 是由服务器发送到浏览器的变量.cookie 通常是服务器嵌入到用户计算机中的小文本文件.每当计算机通过浏览器 ...
- angularjs ng-class 两种用法
ng-class="{'active':current.actived_tree==item}" ng-class="{true:'label-danger white- ...