[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 ----------- ...
随机推荐
- Linux User's Manual IOSTAT
IOSTAT(1) Linux User's Manual IOSTAT(1) NAME iostat - Report Central Processing Unit (CPU) statistic ...
- Object类介绍
一.Object类介绍
- redis配置实例及redis.conf详细说明
一.配置实例 1.redis修改持久化路径.日志路径.清缓存 redis修改持久化路径和日志路径 vim redis.conf logfile /data/redis_cache/logs/redi ...
- HDU 1892-See you(二维BIT)
题意: 最多1000*1000的方格,各方格开始有一本书 有四种操作:对指定方格把书拿走或向里面放书,从一个方格那一定量的书放到另一个方格,查询给定对角线顶点的坐标的矩形范围内包含的书的总数 分析: ...
- codeforces 691F Couple Cover 暴力
分析:开一个300w的数组,统计,然后nlogn统计每个值在在序对第一个出现有多少种情况 时间复杂度:O(nlogn) n在3e6数量级 #include<cstdio> #include ...
- IOS 给图片添加水印 打印文字
1.加文字 -(UIImage *)addText:(UIImage *)img text:(NSString *)text1 { //get image width and ...
- DateTime.ToString格式化日期,使用DateDiff方法获取日期时间的间隔数
一:DateTime.ToString格式化日期 二:代码 using System; using System.Collections.Generic; using System.Component ...
- unity延时方法Invoke和InvokeRepeating
MonoBehaviour里面有两个内置的延时方法 Invoke Invoke(methodName: string, time: float): void; methodName:方法名 time: ...
- asp.net MVC 安全性[笔记]
1. 跨站脚本(XSS) 1.1 介绍 1.1.1 被动注入,利用输入html,javascript 等信息伪造链接,图片等使用提交信息,调转页面等 1.1.2 主动注入,黑客主动参与攻击,不会傻等倒 ...
- 纯CSS3实现宽屏二级下拉菜单
今天我们要来分享一款基于纯CSS3的宽屏二级下拉菜单,这款菜单的子菜单在展开的时候是很宽敞的,菜单项中可以自定义格式的内容,非常实用,也很大气.由于是用纯CSS3实现,所以这款下拉菜单不用运行Java ...