的DataService
import { Injectable } from '@angular/core';
import { Http, RequestOptions, Headers } from "@angular/http";
import { environment } from "../../environments/environment";
import 'rxjs/add/operator/map';
@Injectable()
export class DataService {
public options: RequestOptions;
constructor(protected url: string, protected http: Http) {
}
getAll() {
return this.http.get(this.url, this.options)
.map(
response => response.json()
)
}
get(id: string) {
return this.http.get(this.url + '/' + id, this.options)
.map(response => {
return response;
})
}
create(resource) {
return this.http.post(this.url, resource, this.options)
.map(response => {
return response;
})
}
update(resource) {
return this.http.put(this.url, resource);
}
delete(id: string) {
return this.http.delete(this.url + '/' + id)
}
}
ChildService
import { Injectable } from '@angular/core';
import { DataService } from "../data.service";
import { Http, RequestOptions } from "@angular/http";
import { environment } from "../../../environments/environment";
@Injectable()
export class ChildService extends DataService{
constructor(http:Http) {
super(environment.url,http);
}
}
我正在创建一个服务DataService,其中包含所有http方法(get,create,delete,update),项目中使用的所有服务都在扩展DataService。在DataService的构造函数中,我传递了http对象,来自子类的url通过对父constuctor进行超级(url,http)调用。这样我在最新版本的cli中出现错误,但在旧版本中没有。我得到在 ng build 中使用 ng build --prod 和没有错误构建时出现此错误。可以帮助任何人。
答案 0 :(得分:5)
我发现了问题!
您将DataService标记为@Injectable,但构造函数具有非可注入参数(url:string)。
您必须从提供者列表中删除DataService并删除@Injectable标签