我想用我的配置参数在我的服务中注入一个类。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);
}
您是否尝试过'console.log(this。 _configuration)'? – adriancarriger
我没有影响我的类中的_configuration,它只是我的构造函数的局部属性。 –
@estus我编辑我的问题与大量的数据 –