2017-07-25 29 views
4

我有一個選項卡組:如何緩解標籤點擊?

<ion-tabs> 
    <ion-tab [root]="tab1Root" tabTitle="Programmes" tabIcon="icon-programmes"></ion-tab> 
    <ion-tab [root]="tab2Root" (ionSelect)="studioCheck()" tabTitle="Studio" tabIcon="icon-studio"></ion-tab> 
    <ion-tab [root]="tab3Root" tabTitle="Library" tabIcon="icon-library"></ion-tab> 
    <ion-tab [root]="tab4Root" tabTitle="Profile" tabIcon="icon-profile"></ion-tab> 
</ion-tabs> 

當用戶點擊第二個選項卡(「工作室」),我需要檢查,如果他們有機會獲得它。如果他們這樣做,很好,如果他們不這樣做,我想顯示吐司通知並加載第一個標籤。

我該怎麼做?

我以爲return false就足夠了studioCheck(),但事實並非如此。

打字稿:

studioCheck() { 
    console.log('Studio visited'); 

    // Grab studio selected level 
    this.storage.get('studio_level').then((val) => { 
     console.log('Storage queried'); 
     if(val) { 
     console.log('Level ID: ', val); 
     return true; 
     } else { 
     // No level selected 
     let toast = this.toastCtrl.create({ 
      message: 'Please choose a programme in order to use the Studio', 
      duration: 3000, 
      position: 'top' 
     }); 
     toast.present(); 
     this.navCtrl.parent.select(0); 
     return false; 
     } 
    }); 
    } 

我想,也許ionSelect不等待異步調用storage.get,所以我只是在功能上做了return false;,但也不相同。

任何想法?

回答

0

調整這樣

<ion-tabs #tabs> 
    <ion-tab [root]="tab1Root" tabTitle="Programmes" tabIcon="icon-programmes"></ion-tab> 
    <ion-tab [root]="tab2Root" (ionSelect)="studioCheck()" tabTitle="Studio" tabIcon="icon-studio"></ion-tab> 
    <ion-tab [root]="tab3Root" tabTitle="Library" tabIcon="icon-library"></ion-tab> 
    <ion-tab [root]="tab4Root" tabTitle="Profile" tabIcon="icon-profile"></ion-tab> 
</ion-tabs> 

視圖,然後在控制器:

export class MyPage { 
    @ViewChild("tabs") public tabs: Tabs; 
    ... 

    studioCheck() { 
     console.log('Studio visited'); 

     // Grab studio selected level 
     this.storage.get('studio_level').then((val) => { 
      console.log('Storage queried'); 
      if(val) { 
       console.log('Level ID: ', val); 
       return true; 
      } else { 
       // No level selected 
       let toast = this.toastCtrl.create({ 
        message: 'Please choose a programme in order to use the Studio', 
        duration: 3000, 
        position: 'top' 
       }); 
       toast.present(); 
       this.tabs.select(0); 
      } 
     }); 
    } 
} 

說明

  • 以便在代碼轉換標籤,必須GRAP的Tabs和呼叫實例那就是select
  • 請記住,選擇該選項卡時會發出ionSelect,因此將忽略返回值。
  • 由於this.storage.get,你的代碼是異步,所以return false;不要離開studioCheck方法反正
+0

這仍然具有相同的結果。原因是'this.storage.get'是異步的,因此該選項卡切換而不用等待存儲調用。 – Mike

+0

好耶選項卡開關,但它應該很快返回到第一個選項卡,以防止它受到限制..這就是「this.tabs.select(0)」 –

+0

的目的它失敗,因爲我要切換到的選項卡運行,這是我想要阻止的,因爲存儲器中有數據需要它不存在。我知道我可以在該頁面添加邏輯來防止這種情況發生,但我希望有一種方法可以根本不打開標籤,而不是打開然後關閉它。 – Mike