@ViewChild()使用 (调用子组件方法,实用)

柔情只为你懂 2023-06-25 03:10 59阅读 0赞

原文出处:https://blog.csdn.net/weixin_41952198/article/details/82864890

1、子组件加入个方法

  1. methods(val){
  2. console.log('值为'+val)
  3. }

2、为父组件中的子组件加入变量

  1. <app-viewc #View1></app-viewc>
  2. <app-viewc #View2></app-viewc>

3、父组件控制台

  1. //引入
  2. import { Component, OnInit ,ViewChild} from '@angular/core';
  3. import {ViewcComponent} from '../viewc/viewc.component';
  4. export class ViewFComponent implements OnInit {
  5. @ViewChild('View1') view1:ViewcComponent
  6. @ViewChild('View2') view2:ViewcComponent
  7. constructor() { }
  8. ngOnInit() {
  9. this.view1.methods('第一个值');
  10. this.view2.methods('第二个值');
  11. }
  12. }

变量类型为子组件类,可以使用子组件的方法等,同时触发钩子
ngAfterViewInit,ngAfterViewChecked
————————————————
版权声明:本文为CSDN博主「llaaakk」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin\_41952198/article/details/82864890

发表评论

表情:
评论列表 (有 0 条评论,59人围观)

还没有评论,来说两句吧...

相关阅读