简体   繁体   English

如何在Ionic 3中使用Observables

[英]How to use Observables in ionic 3

Im trying to wait till the subscribe function finish before using the page load completly, but dont know what am doing wrong 我试图等到订阅功能完成后才能完全使用页面加载,但不知道做错了什么

I try this code: 我尝试以下代码:

home.ts home.ts

ionViewDidLoad() {
      this.data.getActiveCars().subscribe(
        notifications => {
              console.log(notifications); // this shows data
              this.notifications = notifications;
              this.ExpiredAssurance = this.notifications.success.ExpiredAssurance;
              this.ExpiredAutorisation = this.notifications.success.ExpiredAutorisation;
          }, error => {
              console.log(error);
          });
        console.log(this.ExpiredAssurance) // i get undefined here 
        console.log(this.ExpiredAutorisation) // i get undefined here 
  }

and in the provider I use this [data provider]: 在提供程序中,我使用以下[数据提供程序]:

getActiveCars():Observable <any>{
    let postData = {
      "user_id": 1,
      "_token": this.token,
      "brand": "getnotification"
  }
    return this.http.post(this.url + "/home",postData,this.httpOptions);
}

and in the home.html also nothing shows when i do this: 当我这样做时,在home.html中也没有任何显示:

<ion-list>
    <ion-item>
      <ion-label>ExpiredAssurance</ion-label>
      <ion-badge>3</ion-badge>
    </ion-item>
    <ion-item>
      <ion-label>ExpiredAutorisation</ion-label>
      <ion-badge slot="end">{{ ExpiredAutorisation }}</ion-badge>
    </ion-item>
</ion-list>

A way to solve that issue it is to hide the result till the response comes. 解决该问题的一种方法是隐藏结果,直到响应出现为止。

<ion-badge *ngIf="ExpiredAssurance" slot="end">{{ ExpiredAssurance }}</ion-badge>

You can also show a loading icon in the meantime 同时,您也可以显示一个加载图标

<load-icon *ngIf="!ExpiredAssurance" />

This element load-icon does not exist but you can download another component to replace it 该元素的load-icon不存在,但是您可以下载另一个组件来替换它

EDIT: Your issue is because of slot="end" property. 编辑:您的问题是由于slot="end"属性。 If you want to use that property you would have to use it inside of the ion-item component: 如果要使用该属性,则必须在ion-item组件内部使用它:

<ion-item>
  <ion-badge slot="start">11</ion-badge>
  <ion-label>My Item</ion-label>
  <ion-badge slot="end">22</ion-badge>
</ion-item>

More info in Ionic documentation ion-badge 有关Ionic文档ion-badge更多信息

I end up finding a solution by adding a loading popup to wait for the subscribe function like this: 我最终找到了一个解决方案,方法是添加一个加载弹出窗口以等待订阅功能,如下所示:

home.ts home.ts

let loader = this.loading.get('loading...');
      loader.present();

      this.data.getActiveCars().subscribe(
        notifications => {
              let data: SuccessObject = new SuccessObject(notifications);
              this.notifications = data.success.success;
              this.ExpiredAssurance = this.notifications.ExpiredAssurance;

              loader.dismiss();

            }, error => {
              console.log(error);
              loader.dismiss();

            });

the loading provider: 加载提供者:

import {Injectable} from '@angular/core';
import {LoadingController} from 'ionic-angular';

@Injectable()
export class LoadingProvider {

    constructor(public loadingCtrl: LoadingController) {
        console.log('Hello LoadingProvider Provider');
    }

    public get(content: string = 'Please wait!') {
        return this.loadingCtrl.create({
            content: content
        })
    }
}

more information about Loading in ionic v3 here 有关在ionic v3中加载的更多信息,请点击此处

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM