我在应用程序的主页上设计了一个元素,当单击时,它会更改活动选项卡。
我曾经做以下操作:
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/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 ?
}
如何触发卡特的点击函数?
看一下此plunkr:https://plnkr.co/edit/kbwa5ybfcyyvqi977acrq?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 {}
我决定以这种方式去做,因为我在github上推荐:
document.getElementById(tabid).click();