2016-12-26 66 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作为声明和导入我的测试模块。如果明天没有人有想法,会试图做一名蹲点运动员。谢谢! –