【问题标题】:How to simulate a click on a ion-tab如何模拟点击离子标签
【发布时间】:2018-03-07 09:25:11
【问题描述】:

我在我的应用程序的主页上设计了一个元素,当点击该元素时,它会更改活动选项卡。

我曾经做过以下事情:

events.subscribe('change-tab', (tab, filterOnSport) => {
      console.log('TabsPage#constructor - change-tab event received with params: ', tab, filterOnSport);
      if (filterOnSport) this.findTabParams.filterOnSport = filterOnSport;
      this.tabs.select(tab);
    });

但如果从未访问过上述选项卡,这在第一次时不起作用(请参阅https://github.com/ionic-team/ionic/issues/12592

我在 Github issue 中看到有人建议模拟点击选项卡。

我正在尝试使用ViewChild这样做,但无法使其工作。

// View
<ion-tabs #tabs>
  <ion-tab [root]="tab1Root" tabIcon="home"></ion-tab>
  <ion-tab #findTab [root]="tab2Root" [rootParams]="findTabParams" tabIcon="search"></ion-tab>
  <ion-tab [root]="tab3Root" tabIcon="chatbubbles" [tabBadge]="unread?1:null" [tabsHideOnSubPages]=true></ion-tab>
  <!--<ion-tab [root]="tab3Root" tabIcon="chatbubbles" [tabBadge]="totalUnreadMessages?.$value" [tabsHideOnSubPages]=true></ion-tab>-->
</ion-tabs>

// Controller
@ViewChild('findTab') findTab: ElementRef;
...
ngAfterViewInit() {
    console.log('TAB elem ', this.findTab);
    // How click the tab ?
  }

如何触发tab的点击功能?

【问题讨论】:

  • 一个疯狂的猜测是:this.findTab.nativeElement.click(); 另一种查找方法是使用浏览器中的 devtool,在 console.log 中破坏您的代码并在控制台中使用 findTab 元素,直到您寻找解决方案

标签: angular ionic2 ionic3 viewchild ionic-tabs


【解决方案1】:

看看这个 plunkr:https://plnkr.co/edit/KBWa5YbFCYYVQI97ACRQ?p=preview

//our root app component
import {Component, NgModule, VERSION, ViewChild} from '@angular/core'
import {BrowserModule} from '@angular/platform-browser'

@Component({
  selector: 'my-app',
  template: `
    <div>
      <h2>Hello {{name}}</h2>
      <button #btn (click)="testClick()">hello</button>
    </div>
  `,
})
export class App implements AfterViewInit {
  @ViewChild('btn') myBtn; 
  name:string;
  constructor() {
    this.name = `Angular! v${VERSION.full}`
  }

  testClick(){
    window.alert("i have been clicked!")
  }

  ngAfterViewInit(){
    this.myBtn.nativeElement.click();
  }
}

@NgModule({
  imports: [ BrowserModule ],
  declarations: [ App ],
  bootstrap: [ App ]
})
export class AppModule {}

【讨论】:

    【解决方案2】:

    我决定按照 GitHub 上的建议走这条路:

    document.getElementById(tabid).click();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-15
      相关资源
      最近更新 更多