Angular 2不透明令牌有什么意义? [英] What is in Angular 2 Opaque Token and What's the Point?

查看:111
本文介绍了Angular 2不透明令牌有什么意义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,在这里,我遇到了不透明令牌"作为在Angular 2中实现全局常量的解决方案:

I am running into 'opaque tokens' as a solution to implementing global constants in Angular 2, for example here: Define global constants in Angular 2

尽管阅读了 docs ,我似乎无法把握要点.

Despite reading the docs, I can't seem to grasp the point.

使用OpaqueToken比使用字符串作为令牌更可取,因为 多个使用同一提供商的提供商可能造成的冲突 字符串作为两个不同的标记.

Using an OpaqueToken is preferable to using strings as tokens because of possible collisions caused by multiple providers using the same string as two different tokens.

什么?首先是什么Angular2代币?我在Google上所获得的只是关于JSON Web令牌(它们在auth等角色中的作用)的答案,据我了解,但显然没有任何关系.

What? What's an Angular2 token to begin with? All I get on google are answers on JSON Web Tokens (their role in auth, etc, etc), which I understand, but are obviously not related in any way.

什么是不透明令牌?它是做什么用的?

What's an Opaque Token? What is it used for?

P.S.关于不透明令牌的更多文档用于提供常量.他们对我没有太大帮助.

P.S. More docs on opaque tokens as used to provide constants. They didn't help me very much, however.

推荐答案

更新Angular4

在Angular4中,不推荐使用OpaqueToken,而将其替换为InjectionToken. InjectionToken允许传递通用类型参数.

In Angular4 OpaqueToken is deprecated and will be replaced by InjectionToken. InjectionToken allows to pass a generic type parameter.

export let APP_CONFIG = new InjectionToken<MyConfig>("app.config");

另请参见

  • https://blog.thoughtram.io/angular/2016/05/23/opaque-tokens-in-angular-2.html
  • https://angular.io/docs/ts/latest/api/core/index/OpaqueToken-class.html (no longer live; rerouted to archive.org version)

原始

什么?什么是Angular2代币?

What? What's an Angular2 token to begin with?

什么是不透明令牌?它是做什么用的?

What's an Opaque Token? What is it used for?

对于Angulars依赖注入的提供者来说,令牌是关键. 提供者已通过键注册,并且由DI实例化的组件,指令和服务类将获得注入的依赖项,这些依赖项将通过提供者密钥进行查找.

A token is a key for providers of Angulars dependency injection. Providers are registered with a key and components, directives, and service classes instantiated by DI get dependencies injected which are looked up by provider keys.

DI支持类型,字符串,OpaqueToken和对象作为键.

DI supports types, strings, OpaqueToken and objects as keys.

export let APP_CONFIG = new OpaqueToken("app.config");

export let APP_CONFIG_2 = {};

providers: [
  MyService, // type is key and value
  {provide: MyService, useClass: MyFancyServiceImpl}, // type is key, `MyFancyServiceImpl` is the value (or rather the information how to create the value
  {provide: 'myservice', useClass: MyService}, // key is a string
  {provide: APP_CONFIG, useValue: {a: 'a', b: 'b'}} // key is an `OpaqueToken`
  {provide: APP_CONFIG_2, useValue: {a: 'a', b: 'b'}} // key is an object

]

// one of these decorators needs to be added to make DI work
@Injectable()
@Component()
@Directive()
@Pipe()
class MyComponent {
  // DI looks up a provider registered with the key `MyService` 
  constructor(private myService: MyService) {} 

  // Same as before but explicit
  constructor(@Inject(MyService) private myService: MyService) {} 

  // DI looks up a provider registered with the key 'myService'
  constructor(@Inject('myservice') private myService: MyService) {} 

  // DI looks up a provider registered with the `OpaqueKey` `APP_CONFIG`
  constructor(@Inject(APP_CONFIG) private myConfig: any) {} 

  // DI looks up a provider registered with the object `APP_CONFIG_2`
  constructor(@Inject(APP_CONFIG_2) private myConfig: any) {} 

对象键(APP_CONFIG_2)和OpaqueToken(APP_CONFIG)必须是完全相同的实例.具有相同内容的其他实例将不起作用.这样可以轻松查找声明密钥的位置以及提供者和注入目标是否使用相同的密钥.

The object key (APP_CONFIG_2) and the OpaqueToken (APP_CONFIG) need to be the exact same instance. A different instance with the same content won't work. This makes it easy to look up where the key is declared and whether the provider and the injection target use the same key.

对于一个字符串,它可能是不同的实例,这带来了风险,即在不同的模块中使用相同的字符串值,可能会导致冲突或注入错误的提供程序.

For a string it can be a different instance, this brings the risk, that the same string value is used in different modules and might cause conflicts or the wrong provider being injected.

这篇关于Angular 2不透明令牌有什么意义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆