Ionic2从Http请求返回一个值并将其赋值给变量 [英] Ionic2 Returning a value from Http request and assign it to variable

查看:192
本文介绍了Ionic2从Http请求返回一个值并将其赋值给变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Ionic2的新手,我真的不明白异步编程是如何工作的。

I am new to Ionic2 and I don't really understand how asynchronous programming works.

我想要函数 checkLogin()返回0或1的值,而是返回下面的结果。如何为变量 this.isLoggedIn 分配我想要的值?

I want function checkLogin() to return the value of 0 or 1, however, instead it returns the result below. How do I make the variable this.isLoggedInto be assigned the value I desire?

任何建议或建议都将不胜感激。

Any advice or suggestion would be appreciated.

提前谢谢。

提供商

checkLogin() {
    var url = this.app.URL + 'api/program/information';
    var isLoggedIn;

    return this.http.get(url).map(res =>res.json()).subscribe( logininfo =>{
        if (!logininfo.data.information.is_logged_in) {
            isLoggedIn = 0;
        } else {
            isLoggedIn = 1;
        }
        return isLoggedIn;
    })
}

home.ts

ionViewWillEnter() {
    this.isLoggedIn = this.globalVar.checkLogin();
    console.log(this.isLoggedIn);
}

结果

推荐答案

您只需从 checkLogin()返回一个observable / code>并在 home.ts 中订阅它。像这样:

You can simply return an observable from checkLogin() and subscribe to it in home.ts. Like this:

checkLogin() {
  var url = this.app.URL + 'api/program/information';
  return this.http.get(url);
}

home.ts:

ionViewWillEnter() {
  this.globalVar.checkLogin()
  .map(res => res.json())
  .subscribe(logininfo => {
    if (!logininfo.data.information.is_logged_in) {
      this.isLoggedIn = 0;
    }
    else {
      this.isLoggedIn = 1;
    }
    console.log(this.isLoggedIn);
  },(err) => {
    console.log("Error occurred:",err);
  });
}

注意:此后所有操作登录信息,将进入 .subscribe() this.globalVar.checkLogin()。如果您尝试访问 this.isLoggedIn 除此之外的任何地方,它可能会也可能无法访问。这就是异步操作。你需要等待它得到解决。其他不依赖于此变量的操作可以在此函数之外结转。

NOTE: All operations which are followed by this login information, will go in the .subscribe() of this.globalVar.checkLogin(). If you will try to access the this.isLoggedIn anywhere else than this, it might or might not be accessible. This is what an async operation is. You need to wait for it to get resolved. Other operations which are not dependent on this variable can carry forward outside of this function.

更新1:转移 if()部分通用方法:

checkLogin() {
  var url = this.app.URL + 'api/program/information';
  return Observable.create(observer => {
    this.http.get(url)
    .map(res => res.json())
    .subscribe(logininfo => {
      var isLoggedIn;

      if(!logininfo.data.information.is_logged_in) {
        isLoggedIn = 0;
      }
      else {
        isLoggedIn = 1;
      }

      observer.next(isLoggedIn);
    },(err) => {
      console.log("Error occurred:",err);
      observer.error(err);
    });
  });
}

home.ts:

ionViewWillEnter() {
  this.globalVar.checkLogin()
  .subscribe(data => {
    this.isLoggedIn = data;
    console.log(this.isLoggedIn);
  },(err) => {
    console.log("Error occurred:",err);
  });
}

这篇关于Ionic2从Http请求返回一个值并将其赋值给变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆