Day 6 AngularJs DI
Don’t lose the link——Robin Milner (ML语言之父)
Components
consume
services; that is, you can inject
a service into a component
, giving the component
access to that service class.
组件使用服务。换句话说,你可以把某个服务注入到组件中,从而使得这个组件可以访问到该服务。
Dependency
injection
(often called DI) is wired into the Angular
framework
and used everywhere to provide
new components
with the services or other things they need.
依赖注入,经常被简称为DI,在Angular中被广泛使用。你几乎可以在任何地方,看见他们为各类组件提供所需的服务。
The injector
maintains
a container
of dependency
instances
that it has already created, and reuses
them if possible.
注入器维护着一个容器,这个容器中是各类已经被创建过的依赖的实例。如果可能的话,注入器会优先复用这些实例。
A provider
is a recipe
for creating a dependency
. For a service, this is typically
the service class itself. For any dependency
you need in your app, you must register
a provider
with the app's injector
, so that the injector
can use it to create new instances
.
“提供者”是创建依赖时所使用的机制。对于普通服务来说,“提供者”通常就是服务类本身。但对于应用中所需的其他依赖来说,你必须通过“注入器”注册一个“提供者”,以便注入器可以使用该“提供者”创建新的实例。
When Angular
discovers
that a component
depends
on a service, it first checks if the injector
already has any existing
instances
of that service.
当Angular发现一个组件依赖于某个服务时,它会先去检查“注入器”中是否已经有了这个服务的实例。
If a requested
service instance
does not yet exist, the injector
makes one using the registered
provider
, and adds it to the injector
before returning the service to Angular
.
如果被请求的服务实例不存在,那么“注入器”就会用注册过的“提供者”创建一个新的实例,然后把它加入到“注入器”中,并把这个实例返回给Angular。
本文选自:
https://angular.io/guide/architecture-services
生词 | 音标 | 释义 |
---|---|---|
consume | [kənˈsu:m] | v. 消耗 |
inject | [ɪnˈdʒɛkt] | v. 注入 |
component | [kəmˈpoʊnənt] | n. 成分;组件 |
dependency | [dɪˈpendənsi] | n. 依赖 |
injection | [ɪnˈdʒɛkʃən] | n. 注入 |
Angular | [ˈæŋɡjəlɚ] | n. 前端JS框架 |
framework | [ˈfreɪmwɜ:rk] | n. 框架 |
provide | [prəˈvaɪd] | vt. 提供 |
injector | [ɪn'dʒektə] | n. 注入器 |
maintain | [menˈten] | v. 维护 |
container | [kənˈtenɚ] | n. 容器 |
instance | [ˈɪnstəns] | n. 实例 |
reuse | [riˈjuz] | v. 重新使用;复用 |
provider | [prəˈvaɪdɚ] | n. 提供者 |
recipe | [ˈresəpi] | n. 秘诀 |
typically | [ˈtɪpɪklɪ] | adv. 通常 |
register | [ˈrɛdʒɪstɚ] | v. 注册 |
discover | [dɪˈskʌvɚ] | v. 发现 |
depend | [dɪˈpɛnd] | v. 依赖 |
existing | [ɪɡˈzɪstɪŋ] | adj. 现存的 |
request | [rɪˈkwɛst] | v. 要求;需要 |
网友评论