2017-08-24 8 views

答えて

1

は単に「噴射」デコレータをインポートし、あなたの答えは、問題を解決しますが、なぜ働いて採掘されていないSO-

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; 
    } 
} 
+0

のように使用し、インジェクタを使用しないでください?私の手技に何が間違っていますか? –

+0

インジェクタは、プリミティブ値ではなく、何かのインスタンスを挿入するために使用されます。変数に割り当てられていないプリミティブ文字列は単なる文字列であり、変数やクラスのインスタンスではありません。 – diopside

+0

コンポーネントの外で宣言した場合、メソッドが動作する可能性があります。 export const currency = ''; を入力してから、指定したプロバイダーの行に を入力してください:通貨(引用符なし) – diopside

関連する問題