1 父组件传子组件
1-1 传值
父组件中 定义值 public title:string = '父组件的值'public title:string = 'nav组件的头部'public msg:string = '父组件的msg'通过<app-nav [titles] = "title"></app-nav>子组件中引入Input后注册@Input() titles !:string使用{{titles}}
1-2 传函数
父组件中定义函数 run(){console.log("父组件的run方法")}通过<app-nav [run] = "title"></app-nav>子组件中引入Input后注册 @Input() run !:any 通过事件触发父组件的事件 <button (click)="dorun()">子组件里获取父组件的run方法</button> dorun(){ this.run() }
3 直接把父组件传递给子组件
通过<app-nav [navs] = "this"></app-nav子组件中引入Input后注册 @Input() navs !:any 通过事件触发父组件的事件 <button (click)="dorun()">子组件里获取父组件的run方法</button> dorun(){ this.nav.run()或者 console.log(this.nav.titles) }