Angular 2 喷油器错误 "Cannot read property 'instance' of undefined"

Angular 2 injector error "Cannot read property 'instance' of undefined"

我有这个组件使用值提供者:

import { Component, OnInit, Injector } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css'],
  providers: [{
    provide: "currency",
    useValue : "dollar"
  }]
})
export class AppComponent implements OnInit  {
  ngOnInit(): void {

  }

title = "app works";


constructor(private injector: Injector){
 this.title = "Currency is: "+ injector.get("currency");
 }
}

当我 运行 程序时,注入器抛出一条错误消息,“Cannot read property 'instance' of undefined

如果重要的话,我的观点很简单<h1>{{title}}</h1>

我做错了什么?

不要使用 Injector,只需导入 'Inject' 装饰器并像这样使用 -

import { Component, OnInit, Inject } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css'],
  providers: [{
     provide: "currency",
     useValue : "dollar"
  }]
})
export class AppComponent implements OnInit  {
  ngOnInit(): void {

  }

  title = "app works";


  constructor(@Inject("currency") private currency){
     this.title = "Currency is: "+ currency;
  }
}