首页 文章

如何减轻标签点击?

提问于
浏览
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>

当用户点击第二个标签('Studio')时,我需要检查他们是否有权访问它 . 如果他们这样做,那很好,如果他们不这样做,我想显示一个Toast通知并加载第一个标签 .

我怎样才能做到这一点?

我认为 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; ,但这样做也是如此 .

有任何想法吗?

1 回答

  • 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);
                }
            });
        }
    }
    

    解释

    • 为了在代码中切换选项卡,您必须抓取 Tabs 的实例并在其上调用 select .

    • 请记住,选中选项卡时会发出 ionSelect ,因此会忽略返回值 .

    • 由于 this.storage.get ,您的代码是异步的,所以 return false; 不要留下 studioCheck 方法

相关问题