在 Angular >= 7 中,使用 @Inject 比声明 class @Injectable 有什么优势吗?

In Angular >= 7, is there any advantage to using @Inject over declaring a class @Injectable?

我看到了一些看起来更老的 Angular 示例,其中依赖注入是使用“@Inject”注释完成的...

import { Component, Inject } from '@angular/core';
import { ChatWidget } from '../components/chat-widget';

@Component({
  selector: 'app-root',
  template: `Encryption: {{ encryption }}`
})
export class AppComponent {
  encryption = this.chatWidget.chatSocket.encryption;

  constructor(@Inject(ChatWidget) private chatWidget) { }
}

在 Angular (>= 7) 的更高版本中,如果被注入的东西被 @Injectable 注释,是否仍然需要 @Inject,例如

@Injectable({
  providedIn: 'root',
})
export class ChatWidget {

我想我想问的是 Angular 的更高版本,是否还有理由继续使用 @Inject?

如果您没有提供

@Inject(SomeClass) 并且您使用 class 作为参数类型,则会在编译阶段自动添加它。在某些情况下,您需要注入一些不是 class 实例的东西。在这种情况下,没有 @Inject 装饰器就不可能注入东西。例如配置对象:

const someConfigObject: ConfigInterface = {...};
...
providers: [
   {provide: MY_CONFIG_TOKEN, useValue: someConfigObject}
]
....
class MyComponent {
   constructor(@Inject(MY_CONFIG_TOKEN) config: ConfigInterface){}
}