0

When I am trying to initialise a variable 'products' as any[], the error is being showed as 'Property 'products' has no initializer and is not definitely assigned in the constructor' . My code is given below. Please help to fix

import { HttpClient } from '@angular/common/http';
import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})
export class AppComponent implements OnInit {
  title = 'Skinet';
  products: any[];  // Error is coming here
  

  constructor(private http: HttpClient){}

  ngOnInit(): void {
    this.http.get('https://localhost:5001/api/products').subscribe((Response: any) => {
      
      this.products = Response.data;
      console.log(Response.data);
    }, error => {
      console.log("Erro");
      console.log(error);
    })
  }

}
Đăng Khoa Đinh
  • 5,038
  • 3
  • 15
  • 33
Alan Pauil
  • 169
  • 3
  • 14
  • 1
    I guess your answer is here: [https://stackoverflow.com/questions/49699067/property-has-no-initializer-and-is-not-definitely-assigned-in-the-construc](https://stackoverflow.com/questions/49699067/property-has-no-initializer-and-is-not-definitely-assigned-in-the-construc) – Anton Marinenko Jun 25 '21 at 07:30
  • Does this answer your question? [Property '...' has no initializer and is not definitely assigned in the constructor](https://stackoverflow.com/questions/49699067/property-has-no-initializer-and-is-not-definitely-assigned-in-the-construc) – Roy Jun 27 '21 at 10:34

0 Answers0