2016-12-26 5 views
0

自分のサービスに、自分の設定パラメータを持つクラスを注入したい。角2がサービスにクラスを注入する

私は、以下のサンプルコードをしましたが、それが動作していません。

import { Config } from '../app.constants'; 

console.log(Config); // here i can access to my properties of config 

@Injectable() 
export class MyService { 


    protected config: Config; 

    constructor(protected _http: Http, @Inject(Config) _configuration: Config) { 
    console.log(this.config); // undefined 
    console.log(_configuration); // undefined 
} 

私はスコープを理解し、私は私の中の私のクラスのコンフィグレーションにアクセスするにはどうすればよいの角2 のプロセスを注入didntのだと思いますサービスMyService?

編集:ここでは

は私のモジュールが

import { NgModule }  from '@angular/core'; 

import { MyService } from './my.service'; 
import { Config } from '../app.constants'; 


@NgModule({ 
    imports: [ 
    ], 
    declarations: [ 
    MyService 
    ], 
    exports: [ 
    MyService 
    ], 
    providers: [ 
    MyService, 
    Config 
    ] 
}) 
export default class MyModule {} 

であり、ここで私の設定です:

import { Injectable } from '@angular/core'; 

@Injectable() 
export class Config { 
    public Port: number = 1234; 
    public Server: string = "http://my-server.com"; 
} 

サービスMYSERVICEが直接呼び出されるが、私はそのように拡張されていません。

@Injectable() エクスポートクラスTestService extends MyS erviceそのようにインポートされた{ ... }

import { TestService } from '../service/test.service'; 
//some modules are from ng2-admin, we can ignore them 
@NgModule({ 
    imports: [ 
    CommonModule, 
    NgaModule, 
    TestRouting 
    ], 
    declarations: [ 
    TestComponent, 
    HoverTable 
    ], 
    exports: [ 
    TestComponent, 
    HoverTable 
    ], 
    providers: [ 
    TestService 
    ] 
}) 
export default class TestModule {} 

、最終的には私のコンポーネント

@Component({ 
    selector: 'test-list', 
    template: require('./test.html') 
}) 
export class TestComponent { 


    constructor(protected service: TestService) { 
     //console.log(service); 

    } 
+0

あなたは 'console.logを試しましたか? _configuration) '? – adriancarriger

+0

私のクラスでは影響を受けていない_configurationは、私のコンストラクタのローカルプロパティです。 –

+0

@estus多くのデータを使って私の質問を編集しました –

答えて

3
export class MyService { 

    protected config: Config; 

    constructor(protected _http: Http, @Inject(Config) _configuration: Config) { 
    console.log(this.config); // undefined 
    console.log(_configuration); // undefined 
    } 
} 

あなたはどこにでも設定フィールドを初期化することはありませんので、それは未定義です。必要なのはMyServiceではサービスですので、それはあなたのモジュールの提供者に、とされていないコンポーネントにある宣言(、パイプに追加する必要があります

export class MyService { 

    protected config: Config; 

    constructor(protected _http: Http, _configuration: Config) { 
    this.config = _configuration; 
    } 
} 

それとも単に

export class MyService { 

    protected config: Config; 

    constructor(protected _http: Http, protected config: Config) { 
    } 
} 

で、ディレクティブ):

@NgModule({ 
    providers: [ 
    MuleService, 
    Config, 
    MyService 
    ] 
}) 

あなただけのサービスではなく、MyServiceでとしてTestServiceを使用したい場合は、TestServiceを注射しなければならないすべての引数を取るコンストラクタを持っていることを確認してください、とo:

constructor(http: Http, config: Config) { 
    super(http, config); 
} 
+0

私のparamの設定は決して設定されていないことに同意します。最初のときに入力したとおりですが、下の行で述べたように、console.log(_configuration);問題は私のコンストラクタの外側にあるので、未定義です。あなたが言ったように私はそれを定義したプロバイダのために、それは私の設定にもアクセスすることはできません。あなたが今言ったように私はTestServiceを編集しようとします! –

+1

それでも問題が解決しない場合は、問題を再現したplunkrを入力してください。 –

+0

私は、テストモジュールの宣言とインポートとしてConfigとMyServiceを追加しても、TestServiceのすべてのパラメータを解決できません。明日のアイデアがなければプランナーをやろうとします。ありがとう! –

関連する問題