@ViewChild()使用

1、子组件加入个方法

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

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



3、父组件控制台

//引入
import { Component, OnInit ,ViewChild} from '@angular/core';
import {ViewcComponent} from '../viewc/viewc.component';


export class ViewFComponent implements OnInit {
  @ViewChild('View1')
  view1:ViewcComponent
  @ViewChild('View2')
  view2:ViewcComponent
  constructor() { }

  ngOnInit() {
    this.view1.methods('第一个值')
    this.view2.methods('第二个值')
  }

}

变量类型为子组件类,可以使用子组件的方法等,同时触发钩子
ngAfterViewInit,ngAfterViewChecked

你可能感兴趣的:(angular)