ng2父组件中触发子组件中的方法@ViewsChild()

发布时间:2019-06-06 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了ng2父组件中触发子组件中的方法@ViewsChild()脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

1、子组件内容

import {component} From @H_360_17@"@Angular/core";
@Component({
  selector: "my-contact",
  templateUrl: "<a (click)='childClick()'>单机事件</a>"
})
export class ChildComponent {
  constructor() {
    console.LOG("contact");
  }

  childClick() {
    alert("子组件的事件触发");
  }
}

2、父组件

import {Component, ViewChild} from "@angular/core";
import {ChildComponent} from "./child.COMponent";
@Component({
  selector: "my-home",
  templateUrl: "<a (click)='parentClick()'>单机,调用子组件的方法</a>"
})
export class ParentComponent {
  @ViewChild(ChildComponent) child: ChildComponent;
    parentClick() {
        this.child.childClick();
    }
}

脚本宝典总结

以上是脚本宝典为你收集整理的ng2父组件中触发子组件中的方法@ViewsChild()全部内容,希望文章能够帮你解决ng2父组件中触发子组件中的方法@ViewsChild()所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。