Angular2呼叫服务
我刚刚开始学习angular2。我在angular2中调用服务时遇到问题。服务调用成功,但我在如何处理数据方面遇到问题。在Angular2呼叫服务,angular,Angular,我刚刚开始学习angular2。我在angular2中调用服务时遇到问题。服务调用成功,但我在如何处理数据方面遇到问题。在Angular1中,我们这样做: DemoService.getExchangeDataConnection().then(function(response){ //here we handle all the data }) test() { return this.http.get('http://...').map(res => res.json());
Angular1
中,我们这样做:
DemoService.getExchangeDataConnection().then(function(response){
//here we handle all the data
})
test() {
return this.http.get('http://...').map(res => res.json());
}
this.service.test().subscribe(data => {
(...)
});
角2
constructor(private _sampleService: SampleService){
this._sampleService = _sampleService;
}
onClickMe(){
this.clickMessage ='You are my hero!';
this.error = "";
this.countries = [];
this._sampleService.test()
.subscribe(
data => this.countries = data,
error => this.error = "Region " + this.region + " is invalid."
);
}
这里我如何处理数据
这是我的服务:
export class SampleService {
constructor(http: Http){
this.http = http;
}
test(){
console.log(AppSettings.API_ENDPOINT);
return this.http.get(AppSettings.API_ENDPOINT+"oceania").toPromise()
.then(res => console.log(res.json()), err => console.log(err));
}
}
如果
test
方法返回一个可观察到的,您需要以与承诺类似的方式订阅它:
this._sampleService.test()
.subscribe(
data => this.countries = data,
error => this.error = "Region " + this.region + " is invalid."
);
例如,对于这样的测试方法:
DemoService.getExchangeDataConnection().then(function(response){
//here we handle all the data
})
test() {
return this.http.get('http://...').map(res => res.json());
}
this.service.test().subscribe(data => {
(...)
});
您可以注意到,您还可以使用Angular2的承诺。在本例中,您将以与之前相同的方式处理响应
编辑
您可以通过以下方式更新测试方法:
test(){
console.log(AppSettings.API_ENDPOINT);
return this.http.get(AppSettings.API_ENDPOINT+"oceania")
.map(res => console.log(res.json());
}
this.service.test().then(data => {
(...)
});
这样称呼它:
DemoService.getExchangeDataConnection().then(function(response){
//here we handle all the data
})
test() {
return this.http.get('http://...').map(res => res.json());
}
this.service.test().subscribe(data => {
(...)
});
如果您想利用promise,您需要在回调中返回一些内容以利用链接:
test(){
console.log(AppSettings.API_ENDPOINT);
return this.http.get(AppSettings.API_ENDPOINT+"oceania").toPromise()
.then(res => res.json(), err => console.log(err)); // <---------
}
如果test
方法返回一个可观察到的,您需要以与承诺类似的方式订阅它:
this._sampleService.test()
.subscribe(
data => this.countries = data,
error => this.error = "Region " + this.region + " is invalid."
);
例如,对于这样的测试方法:
DemoService.getExchangeDataConnection().then(function(response){
//here we handle all the data
})
test() {
return this.http.get('http://...').map(res => res.json());
}
this.service.test().subscribe(data => {
(...)
});
您可以注意到,您还可以使用Angular2的承诺。在本例中,您将以与之前相同的方式处理响应
编辑
您可以通过以下方式更新测试方法:
test(){
console.log(AppSettings.API_ENDPOINT);
return this.http.get(AppSettings.API_ENDPOINT+"oceania")
.map(res => console.log(res.json());
}
this.service.test().then(data => {
(...)
});
这样称呼它:
DemoService.getExchangeDataConnection().then(function(response){
//here we handle all the data
})
test() {
return this.http.get('http://...').map(res => res.json());
}
this.service.test().subscribe(data => {
(...)
});
如果您想利用promise,您需要在回调中返回一些内容以利用链接:
test(){
console.log(AppSettings.API_ENDPOINT);
return this.http.get(AppSettings.API_ENDPOINT+"oceania").toPromise()
.then(res => res.json(), err => console.log(err)); // <---------
}
I get subscribe不是一个函数test
方法返回什么?它返回数据,但我不知道如何处理它可以在您的问题中添加此方法的内容吗?这将更容易帮助您…我得到subscribe不是一个函数,测试
方法返回什么?这将返回数据,但我不知道如何处理此问题可以在您的问题中添加此方法的内容吗?帮助你会更容易…不要把你的http调用转换成承诺。不要把你的http调用转换成承诺。