Angular2中的ViewChild是什么? [英] What is ViewChild in Angular2?

查看:90
本文介绍了Angular2中的ViewChild是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自官方文档。 A ViewChild

Configures a view query.

View queries are set before the ngAfterViewInit callback is called.

解释非常小,我仍然不太明白它用于什么。

The explanation is very minimal and I still don't quite understand what is it used for.

从我找到的博客中考虑这个示例

Consider this example from a blog I found.

取消 @ViewChild(TodoInputCmp) TodoInputCmp中的代码没有影响

有人可以给我一些见解吗?

Can someone give me some insight?

谢谢

推荐答案

它提供了对视图中元素或组件的引用:

It provides a reference to elements or components in your view:

@Component({
  ...
  directives: [SomeComponent],
  template: `
  <div><span #myVar>xxx</span><div>
  <some-comp></some-comp>`
})
class MyComponent {
  @ViewChild('myVar') myVar:ElementRef;
  @ViewChild(SomeComponent) someComponent:SomeComponent;

  ngAfterViewInit() {
    console.log(this.myVar.nativeElement.innerHTML);
    console.log(this.someComponent);
  }
}

变量在 ngAfterViewInit()

这篇关于Angular2中的ViewChild是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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