【问题标题】:How to implement spinner with loading controller in ionic 3?如何在 ionic 3 中使用加载控制器实现微调器?
【发布时间】:2017-09-19 19:08:27
【问题描述】:

我想在 ionic3 中实现带有加载控制器的微调器。我已经实现了简单的加载控制器。怎么做?提前致谢。

我当前的加载器

我想要这样的东西

【问题讨论】:

标签: ionic-framework ionic2 spinner ionic3 loader


【解决方案1】:
presentLoadingCustom() {
    let loading = this.loadingCtrl.create({
      spinner: 'hide',
      content: `<img src="assets/img/gif.gif" />`,
      duration: 5000
    });

    loading.onDidDismiss(() => {
      console.log('Dismissed loading');
    });

    loading.present();
  }

inside image tag 提供一些 gif 图像,效果很好,我已经测试过了

输出

【讨论】:

  • 我是否必须将 标签放入 html 并编写一些 css 才能使用它?
  • @Niraj 请检查我更新的答案,如果你想要任何 gif 图像 giphy.com/gifs/mashable-3oEjI6SIIHBdRxXI40 将此 gif 图像保存在本地并将其放在 assets 文件夹中,一切正常
  • 谢谢,它在放置图像时工作 - let loader = this.loading.create({ spinner: 'hide', content: &lt;img src="assets/img/spinner.png"&gt;, duration: 5000 });
  • 很高兴能帮到你
【解决方案2】:

Ionic 2&3 有一个内置服务,用于在应用程序在后台执行一些耗时的活动(例如从远程数据库加载数据)时阻止 UI 并向用户提供视觉反馈。

您只需使用 ionic-angular 模块中提供的 LoadingController

所以从导入 LadingController 开始

import { LoadingController } from 'ionic-angular';

然后创建一个属性并注入到类构造函数中

export class LoginPage {
loading: any;
constructor(public loadingController:LoadingController){...}
}

并在请求数据的方法中创建加载指示器

login() {
    this.loading = this.loadingController.create({ content: "Logging in ,please wait..." });
    this.loading.present();
    this.errors = "";
    //api service call
    this.authService.postData(this.userData, 'api/account/login').then((result) => {
      this.responseData = result;
      if (result != null) {
        this.loading.dismissAll();
        //console.log(result);
        this.common.setLocalData(DataKey.User.toString(), result);

        this.navCtrl.push(TabsPage);
      }
      else {
        this.loading.dismissAll();
        this.errors = "Nope, Try Again";
      }
    }, (err) => {
      this.loading.dismissAll();
      this.errors = "Nope, Try Again";
      // Error log
    });
  }

当您成功登录时,方法 dismissAll() 会隐藏加载指示器,以便您可以继续与您的应用正常交互。

【讨论】:

  • 我们是否需要在我们用 ionic 创建的每个页面中使用 create 函数?难道我们不能做一个全局提供者,只做两个函数,比如 show() 和 hide()?
  • 是的,我们也在全球供应商中创建。
猜你喜欢
  • 2018-09-09
  • 2016-12-15
  • 1970-01-01
  • 1970-01-01
  • 2016-05-25
  • 1970-01-01
  • 2013-07-24
  • 1970-01-01
相关资源
最近更新 更多