Ionic 2 getComponent alternative

2019-03-04 02:11发布

Since getComponent is no longer available i have the next scenario.

Inside app.ts and app.html i have a menu component and a nav component. Inside the nav component i am adding a page witch contain tabs. When clicking on the menu, which is part of the app component i want to be able to change tabs.

Originally it was easy by using:

this.app.getComponent('mytabs').select(3) Now i understand that i need to use viewchild so i tried using both:

@ViewChild('mytabs') tabs:Tabs;

The ion-tab component has a #mytabs reference.

And

@ViewChild(Tabs) tabs:Tabs;

Both time the this.tabs var is undefined. My guess is that the component is not available when the app.ts component is initialized. What are my options here?

Here is my ionic info

Cordova CLI: 6.1.1
Gulp version: CLI version 3.9.1
Gulp local: Local version 3.9.1
Ionic Framework Version: 2.0.0-beta.7
Ionic CLI Version: 2.0.0-beta.29
Ionic App Lib Version: 2.0.0-beta.16
OS:
Node Version: v4.4.0

1条回答
放我归山
2楼-- · 2019-03-04 02:15

Variables queried by @ViewChild() and @ViewChildren() are not initialized before ngAfterViewInit() was called. If you refer to tabs in the constructor or ngOnInit() it will be null.

查看更多
登录 后发表回答