2017-08-15 9 views
0

Ich versuche, das results Feld aus einer Json Antwort erhalten jedoch erhalte ich die Fehler Property 'results' does not exist on type 'AppComponentProperty ‚Ergebnisse‘ existiert nicht auf Typ ‚AppComponent‘

import { Component } from '@angular/core'; 

//in place where you wanted to use `HttpClient` 
import { HttpClient } from '@angular/common/http'; 



@Component({ 
    selector: 'app-root', 
    templateUrl: './app.component.html', 
    styleUrls: ['./app.component.css'] 
}) 

export class AppComponent { 
    title = 'app'; 

    // Inject HttpClient into your component or service. 
    constructor(private http: HttpClient) {} 
    ngOnInit(): void { 

    // Make the HTTP request: 
    this.http.get('assets/api/list.json').subscribe(data => { 

     // Read the result field from the JSON response. 
     this.results = data['results']; 

    }); 
    } 
} 

Antwort

1

Dies liegt daran, Typoskript Compiler überprüft, dass results Variable existiert/in einer Klasse initialisiert, bevor es in irgendeiner Methode verwendet wird.

export class AppComponent { 
    title = 'app'; 
    results: any[]; //define it here 

    // Inject HttpClient into your component or service. 
    constructor(private http: HttpClient) {} 
    ngOnInit(): void { 

    // Make the HTTP request: 
    this.http.get('assets/api/list.json').subscribe(data => { 

     // Read the result field from the JSON response. 
     this.results = data['results']; 

    }); 
    } 
} 
Verwandte Themen