Home > OS >  Property 'length' does not exist on type 'void' in Angular
Property 'length' does not exist on type 'void' in Angular

Time:07-06

I have this child component slide1.component.ts

import { Component, Input, OnInit, EventEmitter, Output } from '@angular/core';

@Component({
  selector: 'app-slide1',
  templateUrl: './slide1.component.html',
  styleUrls: ['./slide1.component.css'],
})
export class Slide1Component implements OnInit {
  @Input() select_option: string;
  @Output('answer') answer: EventEmitter<{
    Res: any;
    Ans: any;
  }> = new EventEmitter();
  constructor() {}

  ngOnInit() {}
  callchild() {
    var res = this.answer.emit({ Res: '', Ans: '' });
    console.log(res.length);
    console.log('child ');
  }
}

res.length

getting Property 'length' does not exist on type 'void'. but this method which is in parent component returns array and this same function is working when used in parent method but when accessed from child component it is not working.

Working Link : https://stackblitz.com/edit/angular-ivy-gcgxgh?file=src/app/app.component.ts,src/app/slide1/slide1.component.ts

CodePudding user response:

The EventEmitter.emit() method returns void, so its return value has no 'length' property.

I suggest that you read the docs about Components interaction.

Cheers

  • Related