感谢几位的热情解答!
子组件:
export class ChildComponent implements OnInit{
greeting(name:string){
console.log("hello" + name)
}
}
父组件模板:
<app-child #child1></app-child>
父组件控制器:
@ViewChild("child1")
child1:ChildComponent;
ngOnInit(): void{
this.child1.greeting("Tom");
}
child1:ChildComponent; 解释为child1 的类型是 ChildComponent,可以调用子组件的方法。
我的问题是:child1:ChildComponent 这种写法类似继承吗?还是只存在于父子组件关系中的一个本地变量声明?
我概念理解有些混淆。
这段代码的意思是
第一行:你选的
ViewChild
的名字为child1
第二行:你定义了一个名为
child1
的变量
来操作这个ViewChild
,它的类型为ChildComponent(子组件)
其实这样写更为合理