我正在尝试将具有配置文件 (datService.configuration) 的 Angular 服务 (DataService) 加载到组件 (Home) 中。在我的 Home 组件中,如果我不将 DataServiceConfiguration 添加到提供程序列表中,我会收到以下错误:
“未捕获(承诺):DataServiceConfiguration 没有提供者!(Home -> DataService -> DataServiceConfiguration)”
数据服务.ts:
import { Injectable } from 'angular2/core';
import { Http, Response, Headers, HTTP_PROVIDERS } from 'angular2/http';
import 'rxjs/add/operator/map'
import { Observable } from 'rxjs/Observable';
import { DataServiceConfiguration } from './dataService.configuration';
@Injectable()
export class DataService {
private actionUrl: string;
private headers: Headers;
constructor(private _http: Http, private configuration: DataServiceConfiguration) {
...
}
dataService.configuration.ts:
import { Injectable } from 'angular2/core';
@Injectable()
export class DataServiceConfiguration {
public Server: string = "http://localhost/";
public ApiPath: string = "api/v1/";
public ApiUrl = this.Server + this.ApiPath;
constructor() {
}
}
主页.ts
import { DataService } from '../services/dataService';
import { DataServiceConfiguration } from '../services/dataService.configuration';
import {
Headers,
Http,
HTTP_BINDINGS,
Request,
RequestOptions,
RequestOptionsArgs,
HTTP_PROVIDERS
} from 'angular2/http';
@ng.Component({
selector: 'home',
templateUrl: './ng-app/components/public/home/home.html',
directives: [AlbumTile, ProdutoTile, Promocao, SuperOfertas, Banner, Categorias, DestaquesDaHome],
providers: [DataServiceConfiguration, DataService]
})
export class Home implements ng.OnInit {
public lista: models.Lista;
public pagina: number = 1;
paginas: number = 1;
constructor(private http: Http, private _dataService: DataService) {
}
}
我不明白为什么需要将 DataServiceConfiguration 添加到我的 Home 组件中,因为我的 DataService 已经加载了它。有没有办法只将 DataService 添加到我的组件中?