在子组件声明一个方法:
say(meg: string) {
console.log(meg);
}
父组件HTML:
方式一:
<app-test-demo #test></app-test-demo>
ts:
export class AppComponent implements OnInit {
@ViewChild("test") tt: TestDemoComponent;
ngOnInit() {
this.tt.say("dd");
}
}
方式二:
<app-test-demo #test2></app-test-demo>
<button (click) = "test2.say('sdsdsd')">按钮</button>