离子2 getComponent替代 [英] Ionic 2 getComponent alternative
问题描述
由于getComponent不再可用,我有下一个场景。
Since getComponent is no longer available i have the next scenario.
在app.ts和app.html内部我有一个菜单组件和一个导航组件。
在导航组件中,我添加了一个包含制表符的页面。当点击菜单时,该菜单是应用程序组件的一部分,我希望能够更改标签。
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.
最初使用它很容易:
this.app.getComponent('mytabs')。select(3)
现在我明白我需要使用viewchild所以我尝试使用两者:
this.app.getComponent('mytabs').select(3) Now i understand that i need to use viewchild so i tried using both:
@ViewChild('mytabs') tabs:Tabs;
ion-tab组件有#mytabs参考。
The ion-tab component has a #mytabs reference.
和
@ViewChild(Tabs) tabs:Tabs;
两次this.tabs var都是未定义的。我的猜测是,app.ts组件初始化时组件不可用。我有什么选择?
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?
这是我的离子信息
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
推荐答案
查询的变量@ViewChild()
和 @ViewChildren()
在调用 ngAfterViewInit()
之前未初始化。如果您在构造函数中引用 tabs
或 ngOnInit()
,它将 null
。
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
.
这篇关于离子2 getComponent替代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!