import { Component, Input, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { FetchService } from '../fetch. Service';
@Component({
selector: 'app-first',
templateUrl: './first.component.html',
styleUrls: ['./first.component.css']
})
export class FirstComponent implements OnInit {
constructor(private FetchService : FetchService,private router : Router) { }
ngOnInit(): void {
this.getData()
}
@Input() items: any;
getData(){
this.FetchService.fetchData().subscribe(data => {
this.items=data;
console.log(this.items + "first")
},
error => console.error(error)
)
}
second(){
this.router.navigateByUrl("/second")
}
}
In 2nd component HTML and Ts -
<app-first [items]="fetchBirds"></app-first>
export class SecondComponent implements OnInit {
constructor() { }
fetchBirds:any
ngOnInit(): void {
this.data()
}
data(){
console.log(this.fetchBirds + "2nd")
}
}
I have two component 1st and 2nd. I have response which I am able to fetch in 1st component.ts and printed in console. I am using @Input and passing it to 2nd component but its coming as undefined in 2nd component. Please help.