Как смоделировать щелчок на ионной вкладке
Я создал элемент на главной странице своего приложения, который при нажатии изменяет активную вкладку.
Я делал следующее:
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 я видел, что кто-то предложил имитировать щелчок на вкладке.
Я пытаюсь использовать 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 ?
}
Как я могу активировать функцию щелчка на вкладке?
2 ответа
Я решил пойти по этому пути, как мне было рекомендовано на GitHub:
document.getElementById(tabid).click();
Взгляните на этот планер: 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 {}