`providers: [ MyService ]` 是否与 `providers: [{ provide: MyComponent, useClass: MyComponent }]` 相同

Is `providers: [ MyService ]` the same as `providers: [{ provide: MyComponent, useClass: MyComponent }]`

我正在阅读 ng-book angular 2 revision 47 page 250 并且有以下段落:

When we put the class itself into the list of providers like this:

providers: [ MyService ] 

That is telling Angular that we want to provide a singleton instance of MyService whenever MyService is injected. Because this pattern is so common, the class by itself is shorthand notation for the following, equivalent configuration:

providers: [
    { provide: MyComponent, useClass: MyComponent }
]

是否打错了,意思应该是:

providers: [
    { provide: MyService, useClass: MyService }
]

是的,是一样的。 当传递给provideuseClass的类型相同时,相当于只传递type.

只是一个提示。提供组件 (MyComponent) 通常没有意义。

打错了,因为你不能将组件用作提供者,这里是 official docs

The Provider class and provide object literal We wrote the providers array like this:

providers: [Logger]

This is actually a shorthand expression for a provider registration using a provider object literal with two properties:

[{ provide: Logger, useClass: Logger }]

The first is the token that serves as the key for both locating a dependency value and registering the provider.

The second is a provider definition object, which we can think of as a recipe for creating the dependency value. There are many ways to create dependency values ... and many ways to write a recipe

是的,两者是一样的。 当您编写 providers: [MyService] angular 时,会将其扩展为 [{ provide: MyService, useClass: MyService}] 其中第一个参数 provide: MyService 将 用作依赖注入令牌,将用作定位“MyService”的键 第二个参数 useClass: MyService 它将从 现有服务 class 我的服务