gpt4 book ai didi

angular - ionic 2 : How to use ViewChild instead of app. getComponent

转载 作者:太空狗 更新时间:2023-10-29 18:21:28 27 4
gpt4 key购买 nike

我正在将我的 ionic 2 应用程序迁移到删除了 app.getComponent 的 RC 版本。在他们的 github 发行说明中,他们谈到了使用 ViewChild,我该如何正确使用它?

之前(在 RC 版本之前工作):

openPage(page) {
this.app.getComponent('leftMenu').close();
// navigate to the new page if it is not the current page
let nav = this.app.getComponent('nav');
nav.setRoot(page.component);
}

之后:

@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 ORIGINAL EXCEPTION: TypeError: Cannot read property 'close' of undefined

最佳答案

Conference App 中是如何完成的(并稍微更改它以简化代码):

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
});

据我所知,你可以使用MenuController的实例来执行close()方法并隐藏侧边菜单。

如果您需要此代码的 TypeScript 版本,只需添加评论,我会更新答案,不想添加它以保持答案简短。

关于angular - ionic 2 : How to use ViewChild instead of app. getComponent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37694502/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com