前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Angular里useExisting和useClass的使用场景

Angular里useExisting和useClass的使用场景

作者头像
Jerry Wang
发布2020-11-03 15:09:44
1.4K0
发布2020-11-03 15:09:44
举报

StackOverflow上的一个帖子:https://stackoverflow.com/questions/45152995/useclass-vs-useexisting

从上图能看出,所有注入的示例都通过factory方法返回,只是factory方法的实现有所差异。

As we can see in the picture above all of providers can be presented similar useFactory. When it’s the time to get an instance of provider angular just calls factory function.

一些例子:

代码语言:javascript
复制
{provide: Class1, useClass: Class1}, 

等价于:

代码语言:javascript
复制
Class1

而:

代码语言:javascript
复制
{provide: Class1, useClass: Class3}

you can configure, that when a constructor requests Class1 Angular DI creates an instance of Class3 and passes it to the constructor.

当构造函数的参数需要传入Class1时,Angular DI传入一个Class3的框实例到构造函数里。

代码语言:javascript
复制
{provide: Class2, useExisting: Class2}

doesn’t result in an instance being created, but you can see this rather than an alias. If a constructor requests Class2, Angular DI looks for another provider for key Class2 and injects the instance from this Class2 provider. You can see useExisting like a reference to another provider or an alias.

当构造函数需要Class2时,Angular DI从另一个key为Class2的provider里查找,取出对应的实例进行注入。

Injectable and Provider are 2 different things. The injectable is a class DI can create an instance of, or a provided value that might be passed to a constructor. A provider is an entry in a registry of providers that DI maintains and that allows to lookup up providers by key (key is the type of a constructor parameter, a string or an InjectToken). The provider also holds information about how to “create” the injectable value/instance. DI looks up a provider, the provider provides the value, and DI passes the value (instance of an injectable or a value provided as-is) to a constructor.

Injectable和Prodivder是两个不同的概念。Injectable是一个class,Angular DI可以基于该class创建实例,或者DI可以提供一个值,能够传递到constructor里。DI内部维护了一个provider注册表,该注册表支持根据key查询provider.

Key is the type of a constructor parameter, a string or an InjectToken)

key是构造函数的参数,一个字符串或者InjectToken. Provider有足够的knowledge知道如何去创建一个value或者实例。

DI询问provider,provider提供值,DI将值传递到构造函数里。

If you register MyClass as provider, this is the short form of { provide: MyClass, useClass: MyValue } where provide: MyClass is the key a provider can be found with, and useClass: MyValue is a strategy passed to the provider that informs the provider what value it should provide for this key.

provide: MyClass是provider的key,Angular DI注册表里根据这个key找到provider.

useClass: MyValue, 一个传递给provider的strategy,告诉provider对于指定的key,应该提供何种value.

  • useClass: MyValue相当于"use new MyClass(…)"
  • useExisting: Foo

DI需要去查找key为Foo的provider,然后注入Foo provider提供的value.

看一些具体的例子。

UseExisting

在注入ServiceOldS时,使用ServiceNewS这个provider提供的值。

因此,运行时,old和newService两个实例指向的都是newService实例,都是ServiceNewS这个provider注入的实例:

所以最后UI上显示:

useClass

ServiceOldS被注入的是new ServiceNewS,

且和newService的实例不同,所以最后显示new service.

代码语言:javascript
复制
[{ provide: Logger, useClass: Logger }]
  • The provide property holds the token that serves as the key for both locating a dependency value and configuring the injector.
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-10-30 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • UseExisting
  • useClass
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档