Angular 离子2:如何使用ViewChild而不是app.getComponent
我正在将我的ionic 2应用程序迁移到RC版本,其中app.getComponent被删除。 在他们的github发行说明中,他们谈到了如何使用ViewChild,我如何正确使用它 之前(在RC版本之前工作): 之后:Angular 离子2:如何使用ViewChild而不是app.getComponent,angular,typescript,ionic-framework,ionic2,ionic3,Angular,Typescript,Ionic Framework,Ionic2,Ionic3,我正在将我的ionic 2应用程序迁移到RC版本,其中app.getComponent被删除。 在他们的github发行说明中,他们谈到了如何使用ViewChild,我如何正确使用它 之前(在RC版本之前工作): 之后: @Component({ templateUrl: 'build/app.html', queries: { leftMenu: new ViewChild('leftMenu'), nav: new ViewChild('content') } })
@Component({
templateUrl: 'build/app.html',
queries: {
leftMenu: new ViewChild('leftMenu'),
nav: new ViewChild('content')
}
})
....
openPage(page) {
// close the menu when clicking a link from the menu
this.leftmenu.close();
// navigate to the new page if it is not the current page
this.nav.setRoot(page.component);
}
我正在尝试获取“leftMenu”组件,但没有成功。我得到的错误是
browser_adapter.js:77原始异常:TypeError:无法读取
未定义的属性“close”
以下是在中如何完成的(并对其进行一点更改以简化代码): 据我所知,您可以使用MenuController的实例执行
close()
方法并隐藏侧菜单
如果您需要此代码的TypeScript版本,只需添加一条注释,我会更新答案,我不想添加它以保持答案简短。看起来像是@GünterZöchbauer的重复,谢谢您指出。但并没有答案。不,这只是供参考。嗨,若让我们假设我希望得到“leftMenu”的元素,我该怎么做?
@Component({
templateUrl: 'build/app.html',
queries: {
leftMenu: new ViewChild('leftMenu'),
nav: new ViewChild('content')
}
})
....
openPage(page) {
// close the menu when clicking a link from the menu
this.leftmenu.close();
// navigate to the new page if it is not the current page
this.nav.setRoot(page.component);
}
import {Component, ViewChild} from '@angular/core';
import {ionicBootstrap, ..., Platform, MenuController} from 'ionic-angular';
...
@Component({
templateUrl: 'build/app.html',
queries: {
nav: new ViewChild('content')
}
})
class ConferenceApp {
static get parameters() {
return [[...], [Platform], [MenuController]]
}
constructor(..., platform, menu) {
this.menu = menu;
// Call any initial plugins when ready
platform.ready().then(() => {
...
});
openPage(page) {
this.menu.close();
this.nav.setRoot(page);
}
}
ionicBootstrap(ConferenceApp, [...], {
// config
});