博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Angular 2] Understanding OpaqueToken
阅读量:7095 次
发布时间:2019-06-28

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

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';@Injectableexport 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'  }]

 

转载地址:http://pnhql.baihongyu.com/

你可能感兴趣的文章
在机器关机时关闭mysql服务实例
查看>>
micro:bit 的完整硬件方案
查看>>
用Class ID做网络分流
查看>>
nginx 502错误
查看>>
VS2013环境下Boost库配置
查看>>
F5多出口配置
查看>>
Android Studio 第六十二期 - Android框架
查看>>
PCIE hotplug 调试小结
查看>>
我的友情链接
查看>>
输出二维环形数组中最大子数组和
查看>>
用户调查报告
查看>>
servlet 单例多线程
查看>>
Linux命令模拟Http的get或post请求
查看>>
Navicat使用教程:使用Navicat Query Analyzer优化查询性能(第2部分)
查看>>
mongoDB 在windows平台下安装成系统服务
查看>>
linux学习第八周总结
查看>>
第二次测试题
查看>>
Java 处理异常 9 个最佳实践,你知道几个?
查看>>
Apache 不能列目录解决。
查看>>
如何永久的修改主机名
查看>>