Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 离子2:如何使用ViewChild而不是app.getComponent_Angular_Typescript_Ionic Framework_Ionic2_Ionic3 - Fatal编程技术网

Angular 离子2:如何使用ViewChild而不是app.getComponent

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

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

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

之后:

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