离子 2 getComponent 替代品 [英] Ionic 2 getComponent alternative

查看:15
本文介绍了离子 2 getComponent 替代品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于 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 变量都是未定义的.我的猜测是,当 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() 被调用.如果您在构造函数或 ngOnInit() 中引用 tabs,它将是 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆