2016-12-26 60 views
0

我想用我的配置參數在我的服務中注入一個類。Angular 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. 過程如何訪問我的課配置裏面我服務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() export class 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(this。 _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 
    } 
} 

你從來沒有在任何地方初始化配置領域,所以它是未定義。所有你需要的是

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) { 
    } 
} 

由於爲MyService是,必須將其添加到您的模塊的供應商,而不是聲明(這是件,管道和服務指令):

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

如果你只想使用TestService的作爲一種服務,而不是爲MyService,確保TestService的有一個構造函數都必須被注入的參數,以o:

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

我同意我的param配置從未設置,在開始時我正在填充它,因爲你做了,但如在下面的行說,甚至console.log(_configuration);未定義,所以問題在我的構造函數之外。對於我所定義的提供者也如你所說,但即使如此,也無法訪問我的配置。我會嘗試編輯TestService,就像你現在說的那樣! –

+1

如果它仍然不起作用,那麼提供一個plunkr來重現問題。 –

+0

我有無法解析TestService的所有參數,即使當我添加Config和MyService作爲聲明和導入我的測試模塊。如果明天沒有人有想法,會試圖做一名蹲點運動員。謝謝! –