在父组件中获取子组件的引用

Getting reference to child component in parent component

在 Angular 2 中,我有一个具有子组件的组件。但是,我想获取该子组件的副本以在父组件中使用,以调用其函数或其他任何内容。

我发现我可以使用局部变量,这样我就可以使用模板中的组件了。但是,我不只是在模板中使用它,我想在组件的实际代码中使用它。

我找到了一种方法,这是子代码:

//our child
import {Component, OnInit, EventEmitter} from 'angular2/core'

@Component({
  selector: 'my-child',
  providers: [],
  template: `
    <div>
      <h2>Child</h2>

    </div>
  `,
  directives: [],
  outputs: ['onInitialized']
})

export class Child implements OnInit{

  onInitialized = new EventEmitter<Child>();

  constructor() {
    this.name = 'Angular2'
  }

  ngOnInit() {
    this.onInitialized.emit(this);
  }
}

家长:

//our root app component
import {Component} from 'angular2/core'
import {Child} from './child'

@Component({
  selector: 'my-app',
  providers: [],
  template: `
    <div>
      <h2>Hello {{name}}</h2>
      <my-child (onInitialized)="func($event)"></my-child>
    </div>
  `,
  directives: [Child]
})
export class App {
  constructor() {
    this.name = 'Angular2'
  }

  func(e) {
    console.log(e)

  }
}

我在 this plunker 中实现了它。但这似乎是一个黑客。

没有更简单的方法可以将组件附加到其父级中的变量吗?

您可以使用ViewChild

<child-tag #varName></child-tag>

@ViewChild('varName') someElement;

ngAfterViewInit() {
  someElement...
}

其中 varName 是添加到元素的模板变量。或者,您可以按组件或指令类型查询。

还有其他选择,例如 ViewChildren, ContentChild, ContentChildren

@ViewChildren也可以在构造函数中使用

constructor(@ViewChildren('var1,var2,var3') childQuery:QueryList)

好处是出结果比较早

另请参阅 http://www.bennadel.com/blog/3041-constructor-vs-property-querylist-injection-in-angular-2-beta-8.htm,了解一些 advantages/disadvantages 使用构造函数或字段的信息。

注意:@Query()@ContentChildren()

的弃用前身

更新

Query目前只是一个抽象基础class。完全没发现有没有用https://github.com/angular/angular/blob/2.1.x/modules/@angular/core/src/metadata/di.ts#L145

你实际上可以选择 ViewChild API...

parent.ts

<button (click)="clicked()">click</button>

export class App {
  @ViewChild(Child) vc:Child;
  constructor() {
    this.name = 'Angular2'
  }

  func(e) {
    console.log(e)

  }
  clicked(){
   this.vc.getName();
  }
}

child.ts

export class Child implements OnInit{

  onInitialized = new EventEmitter<Child>();
  ...  
  ...
  getName()
  {
     console.log('called by vc')
     console.log(this.name);
  }
}

您需要利用 @ViewChild 装饰器通过注入从父组件引用子组件:

import { Component, ViewChild } from 'angular2/core';  

(...)

@Component({
  selector: 'my-app',
  template: `
    <h1>My First Angular 2 App</h1>
    <child></child>
    <button (click)="submit()">Submit</button>
  `,
  directives:[App]
})
export class AppComponent { 
  @ViewChild(Child) child:Child;

  (...)

  someOtherMethod() {
    this.searchBar.someMethod();
  }
}

这是更新的 plunkr:http://plnkr.co/edit/mrVK2j3hJQ04n8vlXLXt?p=preview

您会注意到 @Query 参数装饰器也可以使用:

export class AppComponent { 
  constructor(@Query(Child) children:QueryList<Child>) {
    this.childcmp = children.first();
  }

  (...)
}