Angular8 子组件向父组件传递参数

废话少说,上代码

父组件 html

注意 (childSelectIndex)要和子组件output的名字一致

  1. <keepbook-add (childSelectIndex)="onChildSelectIndexChange($event)"></keepbook-add>

父组件TS

  1. import { Component, OnInit } from '@angular/core';
  2. @Component({
  3. selector: 'app-keepbook-index',
  4. templateUrl: './index.component.html',
  5. styleUrls: ['./index.component.less']
  6. })
  7. export class IndexComponent implements OnInit {
  8. constructor() { }
  9. ngOnInit() {
  10. }
  11. /**
  12. * 子组件和父组件通信
  13. * @param event
  14. */
  15. onChildSelectIndexChange(event){
  16. console.log(event);
  17. }
  18. }

子组件TS

关键代码,向父组件传值
@Output() childSelectIndex : EventEmitter<any> = new EventEmitter();

  1. import { Component, OnInit, Output, EventEmitter } from '@angular/core';
  2. @Component({
  3. selector: 'keepbook-add',
  4. templateUrl: './add.component.html',
  5. styleUrls: ['./add.component.less']
  6. })
  7. export class AddComponent implements OnInit {
  8. //
  9. @Output() childSelectIndex : EventEmitter<any> = new EventEmitter();
  10. constructor() { }
  11. ngOnInit() {
  12. }
  13. /**
  14. 调用该方法传递值给父组件
  15. **/
  16. onOk() {
  17. this.childSelectIndex.emit('传递给父组件的值');
  18. }
  19. }