最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Property 'results' does not exist on type 'AppComponent' - Stack Overflow

programmeradmin0浏览0评论

I am trying to get the results field from a json response however I get the error Property 'results' does not exist on type 'AppComponent

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

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



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

export class AppComponent {
  title = 'app';

  // Inject HttpClient into your ponent 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'];

    });
  }
}

I am trying to get the results field from a json response however I get the error Property 'results' does not exist on type 'AppComponent

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

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



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

export class AppComponent {
  title = 'app';

  // Inject HttpClient into your ponent 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'];

    });
  }
}
Share Improve this question asked Aug 15, 2017 at 16:59 mruss24mruss24 3672 gold badges7 silver badges14 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

This is because Typescript piler checks that results variable exists/initialized in a class, before using it in any method.

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

  // Inject HttpClient into your ponent 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'];

    });
  }
}

Sometimes ['results'] will log the same error. (maybe for newer angular/ts versions)

 this.http.get(url).subscribe((data:any) => {

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

评论列表(0)

  1. 暂无评论