我想用我的配置參數在我的服務中注入一個類。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我編輯我的問題與大量的數據 –