我有一个 API 类:
export class ApiService {
constructor(public http: Http) { }
put(controller: string, method: string, data: Object) {
return this.http.put("http://127.0.0.1:8000",
JSON.stringify(data), {
})
.map(res => res.json())
.catch(err => {
return Observable.throw(err.json());
});
}
}
和一个 AccountService 类:
export class AccountService {
api: ApiService;
constructor() {
this.api = new ApiService();
}
login(username: string, password: string) {
return this.api.put("accounts", "login", { username: username, password: password});
}
}
但是,当我运行此示例时,存在两个问题:
1)ApiService
构造函数中需要http。因此this.api = new ApiService();
应该提供Http
哪些不是我想要的。
我怎样才能修改,ApiService
所以我不必提供Http
给构造函数?
2)在AccountService
方法this.api.put
上没有找到ApiService
。自从我实例化之后我不ApiService
明白this.api