【问题标题】:Ionic 2, Tab content gets rendered before method execution in main page is completedIonic 2,在主页面中的方法执行完成之前呈现选项卡内容
【发布时间】:2017-12-05 16:55:06
【问题描述】:

我有一个仪表板页面。这个页面有两个标签来显示最近的销售和最近的库存。 当用户交互仪表板时,loadDashboardItems 方法被调用。

我的 dashboard.ts

import { Component } from '@angular/core';
import {NavController, NavParams, LoadingController } from 'ionic-angular';
import {Storagehelper} from '../../providers/storagehelper';
import { DashboardrecentsalesPage } from '../dashboardrecentsales/dashboardrecentsales';
import { DashboardrecentinventoryPage } from '../dashboardrecentinventory/dashboardrecentinventory';
import {Webservice} from '../../providers/webservice';
/**
 * Generated class for the Dashboard page.
 *
 * See http://ionicframework.com/docs/components/#navigation for more info
 * on Ionic pages and navigation.
 */

@Component({
  selector: 'page-dashboard',
  templateUrl: 'dashboard.html',
})
export class DashboardPage {
  private recentSales;
  private recentInventory;
  private loading;
  private ajaxRequest;


  constructor(public navCtrl: NavController, public navParams: NavParams, private storagehelper: Storagehelper, public loadingCtrl: LoadingController, public webservice: Webservice) {

      this.loadDashboardItems();


      this.recentSales = DashboardrecentsalesPage; //This is the default tab in dashboard page
      this.recentInventory = DashboardrecentinventoryPage;


  }


  ionViewWillLeave(){
    if(this.loading) this.loading.dismiss().catch(() => {});

    if(this.ajaxRequest!=undefined){
        this.ajaxRequest.unsubscribe();
    }
  }



  private loadDashboardItems(){
      //HERE API REQUEST IS MADE AND data is saved to Localstorage

  }

}

和默认标签 dashboardrecentsales.ts

import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams, Tabs } from 'ionic-angular';
import {Storagehelper} from '../../providers/storagehelper';
/**
 * Generated class for the Dashboardsummaryitems page.
 *
 * See http://ionicframework.com/docs/components/#navigation for more info
 * on Ionic pages and navigation.
 */

@Component({
  selector: 'page-dashboardrecentsales',
  templateUrl: 'dashboardrecentsales.html',
})
export class DashboardrecentsalesPage {
      private RecentItems;

      constructor(public navCtrl: NavController, public navParams: NavParams, private storagehelper: Storagehelper) {
            this.getDashboardItems();
      }

      getDashboardItems(){
            this.RecentItems = this.storagehelper.getStorageItem("RecentItems");
            //This RecentItems property is used to render view dashboardrecentsales
      }



}

dashboard.ts 中发出 API 请求并将数据保存在 LocalStorage

“dashboardrecentsales.ts”从本地存储中获取数据并呈现视图以显示在选项卡上。

问题

我遇到的问题是,如果用户第一次访问dashboard页面,即使数据保存在localstorage中,标签页也没有从localstorage中获取任何数据。

似乎在仪表板页面的loadDashboardItems方法执行完成之前呈现了Tab。

我试着放了

this.recentSales = DashboardrecentsalesPage; //This is the default tab in dashboard page
      this.recentInventory = DashboardrecentinventoryPage;

loadDashboardItems 方法中,所有内容都保存到本地存储,但根本没有呈现选项卡视图。

任何人都可以建议我在这种情况下可以做什么。

【问题讨论】:

    标签: ionic2


    【解决方案1】:

    无论如何,我使用event.publishevent.subscribe 解决了它。更多信息Here

    链接中的示例代码。我用这个逻辑来刷新内容。

    import { Events } from 'ionic-angular';
    
    // first page (publish an event when a user is created)
    constructor(public events: Events) {}
    createUser(user) {
      console.log('User created!')
      events.publish('user:created', user, Date.now());
    }
    
    
    // second page (listen for the user created event after function is called)
    constructor(public events: Events) {
      events.subscribe('user:created', (user, time) => {
        // user and time are the same arguments passed in `events.publish(user, time)`
        console.log('Welcome', user, 'at', time);
      });
    }
    

    【讨论】:

      【解决方案2】:

      对于您的情况,您可以使用 ionViewDidLoad() 来加载仪表板项目。试试这样的,

      ionViewDidLoad(){
          this.loadDashboardItems();
      }
      

      查看this documentation 以了解有关所有预定义 NavController 方法的更多信息

      希望有帮助!

      【讨论】:

      • 已经试过了,不行。在 loadDashboardItems() 执行完成之前,选项卡仍会呈现。
      • 你想达到什么目的?创建一个提供程序,在那里加载您的 api 数据并将数据引入您需要的任何页面
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-04-19
      • 2021-01-06
      • 1970-01-01
      • 2016-05-07
      • 2017-06-07
      • 1970-01-01
      • 2017-08-03
      相关资源
      最近更新 更多