import { Component, OnInit, ViewChild } from '@angular/core';@Component({ selector: 'app-child', templateUrl: './child.component.html', styleUrls: ['./child.component.css']})export class ChildComponent implements OnInit { constructor() { } ngOnInit() { } greeting(str: string) { console.log(str); }}
child works!
import { Component, ViewChild } from '@angular/core';import { ChildComponent } from './child/child.component';@Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css']})export class AppComponent { @ViewChild('child1') child1: ChildComponent; constructor() { } // tslint:disable-next-line:use-life-cycle-interface ngOnInit(): void { this.child1.greeting('child1的问候'); }}