1
我有利用价值提供这个组件:角2喷油器错误“无法读取的未定义的属性‘实例’”
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>
。
我做错了什么?
你的回答可以解决问题,但为什么不是我的工作?我的程序有什么问题? –
注入器用于注入某些事物的实例,而不是原始值。未分配给变量的原始字符串只是一个字符串,而不是变量或类的实例。 – diopside
如果在组件之外声明 export const currency =''; 然后在您指定的提供商行 提供:currency(without the quotes) 但我不确定 – diopside