1 父组件传子组件

1-1 传值

  1. 父组件中
  2. 定义值 public title:string = '父组件的值'
  3. public title:string = 'nav组件的头部'
  4. public msg:string = '父组件的msg'
  5. 通过<app-nav [titles] = "title"></app-nav>
  6. 子组件中引入Input后注册
  7. @Input() titles !:string
  8. 使用{{titles}}

1-2 传函数

  1. 父组件中
  2. 定义函数 run(){console.log("父组件的run方法")}
  3. 通过<app-nav [run] = "title"></app-nav>
  4. 子组件中引入Input后注册
  5. @Input() run !:any
  6. 通过事件触发父组件的事件
  7. <button (click)="dorun()">子组件里获取父组件的run方法</button>
  8. dorun(){
  9. this.run()
  10. }

3 直接把父组件传递给子组件

  1. 通过<app-nav [navs] = "this"></app-nav
  2. 子组件中引入Input后注册
  3. @Input() navs !:any
  4. 通过事件触发父组件的事件
  5. <button (click)="dorun()">子组件里获取父组件的run方法</button>
  6. dorun(){
  7. this.nav.run()或者
  8. console.log(this.nav.titles)
  9. }