Javascript 当调用成功时,如何从angular 2中的http调用返回数据
以该函数为例:Javascript 当调用成功时,如何从angular 2中的http调用返回数据,javascript,angularjs,promise,angular,Javascript,Angularjs,Promise,Angular,以该函数为例: getSessionInfo() { this._http.get(someCorrectUrlHere) // Don't know what this map function do .map(res => res.json()) // Don't know what this subscribe function do .subscribe( data => { if (data.respo
getSessionInfo() {
this._http.get(someCorrectUrlHere)
// Don't know what this map function do
.map(res => res.json())
// Don't know what this subscribe function do
.subscribe(
data => {
if (data.response.statusCode === 200) {
** the data.response.data in this case is an object**
return data.response.data;
} else {
console.log("cant get session info");
}
},
err => console.log(err),
() => { }
);
}
我的理解是,调用getSessionInfo()时,返回data.response.data
实际上不会返回某些内容
例如,this.session仍将是未定义的
getSession() {
this.session = this.service.getSessionInfo();
}
我要做的是,返回值data.response.data并将其分配给this.session
注意,它们在Angular2项目中属于不同的类别
export class HeaderComponent {
service: ApiService;
session: Object;
constructor(service: ApiService) {
this.service = service;
this.getSession();
}
getSession() {
this.session = this.service.getSessionInfo();
// I expect this is the true value I expected, not undefined
console.log(this.session);
}
}
ApiService类位于其他文件夹中
@Injectable()
export class ApiService {
_http: Http;
regions: Object;
constructor(http: Http) {
this._http = http;
}
getSessionInfo() {
.......
}
}
我以前知道他们可以使用$q、$DEBER做我想做的事情,但是我应该使用Angular 2做什么呢?请返回创建一个新的承诺,就像你将要使用的那样。所以
this.\u http.get(somecorrectorurlhere)
从getSessionInfo
方法获得承诺,然后确实有。然后通过getSessionInfo
方法使用箭头函数
getSessionInfo() {
//creating custom promise & then resolving and rejecting it on condition.
return new Promise((resolve, reject) =>{
this._http.get(someCorrectUrlHere)
.map(res => res.json())
.subscribe(
data => {
if (data.response.statusCode === 200) {
resolve(data.response.data); //returning data by resolving promise
} else {
console.log("cant get session info");
reject("Error occured");
}
},
err => {
console.log(err);
reject(err);
},
() => {}
);
});
}
代码
export class HeaderComponent {
service: ApiService;
session: Object;
constructor(service: ApiService) {
this.service = service;
this.getSession();
}
getSession() {
this.service.getSessionInfo().then(
//success function.
(session) => {
this.session = session;
console.log(session);
},
(error)=>{
//error function
});
}
}
当您要使用时,请创建新承诺
/可观察
。订阅可观察。所以this.\u http.get(somecorrectorurlhere)
从getSessionInfo
方法获得承诺,然后确实有。然后通过getSessionInfo
方法使用箭头函数
getSessionInfo() {
//creating custom promise & then resolving and rejecting it on condition.
return new Promise((resolve, reject) =>{
this._http.get(someCorrectUrlHere)
.map(res => res.json())
.subscribe(
data => {
if (data.response.statusCode === 200) {
resolve(data.response.data); //returning data by resolving promise
} else {
console.log("cant get session info");
reject("Error occured");
}
},
err => {
console.log(err);
reject(err);
},
() => {}
);
});
}
代码
export class HeaderComponent {
service: ApiService;
session: Object;
constructor(service: ApiService) {
this.service = service;
this.getSession();
}
getSession() {
this.service.getSessionInfo().then(
//success function.
(session) => {
this.session = session;
console.log(session);
},
(error)=>{
//error function
});
}
}
您可以将Angular 2 http调用返回的Observable转换为Promise,前提是您可以轻松地使用它
getSessionInfo() {
return this._http.get(someCorrectUrlHere).toPromise();
);
更多关于它的官方文件
或者,你可以试着用两种方式来做。保持您的服务作为
getSessionInfo() {
this._http.get(someCorrectUrlHere)
.map(res => res.json())
}
订阅你的HeaderComponent
getSession() {
this.service.getSessionInfo().subscribe(
data => {
if (data.response.statusCode === 200) {
this.session = data.response.data;
console.log(this.session);
} else {
console.log("cant get session info");
}
},
err => console.log(err),
() => { }
);
}
您可以将Angular 2 http调用返回的Observable转换为Promise,前提是您可以轻松地使用它
getSessionInfo() {
return this._http.get(someCorrectUrlHere).toPromise();
);
更多关于它的官方文件
或者,你可以试着用两种方式来做。保持您的服务作为
getSessionInfo() {
this._http.get(someCorrectUrlHere)
.map(res => res.json())
}
订阅你的HeaderComponent
getSession() {
this.service.getSessionInfo().subscribe(
data => {
if (data.response.statusCode === 200) {
this.session = data.response.data;
console.log(this.session);
} else {
console.log("cant get session info");
}
},
err => console.log(err),
() => { }
);
}
它说“属性然后不存在于类型订阅”,谢谢现在我在我的项目中使用你的方法!非常感谢,很高兴认识你。ThanksIt说“属性然后不存在于类型订阅上”,谢谢现在我在我的项目中使用您的方法!非常感谢,很高兴认识你。谢谢,我想我必须在getSessionInfo()中添加return吗。我没有调查细节。我只是建议你可以采取两种方法。看看我添加的docs链接。我想我必须在getSessionInfo()中添加return吗。我没有调查细节。我只是建议你可以采取两种方法。看看我添加的文档链接。