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.

We created a value provide like this:
 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的更多相关文章

  1. [Angular 2] Understanding Pure & Impure pipe

    First, how to use a build in pipe: <div class="pipe-example"> <label>Uppercase ...

  2. [Angular 2] Understanding @Injectable

    In order to resolve a dependency, Angular’s DI uses type annotations. To make sure these types are p ...

  3. 来自 Thoughtram 的 Angular 2 系列资料

    Angular 2 已经正式 Release 了,Thoughtram 已经发布了一系列的文档,对 Angular 2 的各个方面进行深入的阐释和说明. 我计划逐渐将这个系列翻译出来,以便对大家学习 ...

  4. angular default project (angular.json的解读)

    Change the default Angular project Understanding it's purpose and limits Klaus KazlauskasFollow Nov ...

  5. Angular 4+ 修仙之路

    Angular 4.x 快速入门 Angular 4 快速入门 涉及 Angular 简介.环境搭建.插件表达式.自定义组件.表单模块.Http 模块等 Angular 4 基础教程 涉及 Angul ...

  6. Angular复习笔记6-依赖注入

    Angular复习笔记6-依赖注入 依赖注入(DependencyInjection)是Angular实现重要功能的一种设计模式.一个大型应用的开发通常会涉及很多组件和服务,这些组件和服务之间有着错综 ...

  7. angular2 学习笔记 ( DI 依赖注入 )

    refer : http://blog.thoughtram.io/angular/2016/09/15/angular-2-final-is-out.html ( search Dependency ...

  8. AngularJs学习笔记--Understanding Angular Templates

    原版地址:http://docs.angularjs.org/guide/dev_guide.mvc.understanding_model angular template是一个声明规范,与mode ...

  9. 【转】Understanding the Angular Boot Process

    原文: https://medium.com/@coderonfleek/understanding-the-angular-boot-process-9a338b06248c ----------- ...

随机推荐

  1. JMeter重要知识点汇总

    1)现在对于JMeter来说,一个测试计划只能有一个cookie管理器.因为当多个manager存在时,JMeter目前还没有方法来指定使用哪个manager.同时,一个cookie manager中 ...

  2. Java进程占用CPU资源过多分析

    问题描述: 生产环境下的某台tomcat7服务器,在刚发布时的时候一切都很正常,在运行一段时间后就出现CPU占用很高的问题,基本上是负载一天比一天高. 问题分析: 1,程序属于CPU密集型,和开发沟通 ...

  3. Cloudera的安装

    To enable these parts of the tutorial, choose one of the following options: To use Cloudera Express ...

  4. 在asp.net mvc中使用PartialView返回部分HTML段

    问题链接: MVC如何实现异步调用输出HTML页面 该问题是个常见的 case, 故写篇文章用于提示新人. 在asp.net mvc中返回View时使用的是ViewResult,它继承自ViewRes ...

  5. 瞬间从IT屌丝变大神——HTML规范

    HTML规范包含以下内容: DTD统一用<!DOCTYPE HTML PUBLIC "_//W3C//DTD XHTML 1.0 Transitional//EN"" ...

  6. well-posed problem and ill-posed problem

    well-posed problem must have the property that A solution exists The solution is unique The solution ...

  7. Bias/variance tradeoff

    线性回归中有欠拟合与过拟合,例如下图: 则会形成欠拟合, 则会形成过拟合. 尽管五次多项式会精确的预测训练集中的样本点,但在预测训练集中没有的数据,则不能很好的预测,也就是说有较大的泛化误差,上面的右 ...

  8. [Windows]VS2010如何以管理员权限启动?(转)

    在某些项目进行开发的时候,需要提升应用程序本身的权限,这个是很容易的.但是如何让VS2010启动的时候就已管理员权限运行程序呢?为这个问题苦恼了好久,终于找到了办法. 找到VS2010的快捷方式:右击 ...

  9. 5个让人激动的Java项目

    每个Java/JVM领域的技术专家都应从那些项目中获益,他们中有2011在SanJose举办的Jax 会议中提名为最好的技术产品奖的.我之所以选择它们,是因为它们可以广泛用于一系列的项目中,解决真实问 ...

  10. 30 分钟 Java Lambda 入门教程

    Lambda简介 Lambda作为函数式编程中的基础部分,在其他编程语言(例如:Scala)中早就广为使用,但在Java领域中发展较慢,直到java8,才开始支持Lambda. 抛开数学定义不看,直接 ...