2017-08-15 21 views
0

私はしかし、私はProperty 'results' does not exist on type 'AppComponentプロパティ「結果」タイプに存在しません「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']; 

    }); 
    } 
} 

答えて

1

これは活字体コンパイラのチェックがresults変数が存在していることため、エラーを取得したJSONレスポンスからresultsフィールドを取得しようとしています/クラス内で初期化された後、どのメソッドでも使用されます。

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']; 

    }); 
    } 
} 
関連する問題