Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Html 在angular 7中处理JSON数据_Html_Json_Angular_Typescript - Fatal编程技术网

Html 在angular 7中处理JSON数据

Html 在angular 7中处理JSON数据,html,json,angular,typescript,Html,Json,Angular,Typescript,所以我对Angular、html和Typescript非常陌生。。。 说到这里,我将学习本教程 我已经在控制台中很好地显示了JSON数据。然而,在24:12的视频中,我得到了与他相同的错误,只是它不起作用 我试图将城市名称从home.component.ts推送到控制台,但仍然收到相同的错误 这里有一些代码可以解释我的愚蠢,谢谢 home.component.ts文件 import { Component, OnInit } from '@angular/core'; import {Weath

所以我对Angular、html和Typescript非常陌生。。。 说到这里,我将学习本教程

我已经在控制台中很好地显示了JSON数据。然而,在24:12的视频中,我得到了与他相同的错误,只是它不起作用

我试图将城市名称从home.component.ts推送到控制台,但仍然收到相同的错误

这里有一些代码可以解释我的愚蠢,谢谢

home.component.ts文件

import { Component, OnInit } from '@angular/core';
import {WeatherService} from '../weather.service';


@Component({
  selector: 'app-home',
  templateUrl: './home.component.html',
  styleUrls: ['./home.component.css']
})
export class HomeComponent implements OnInit {


  location = {
    city: 'orlando',
    code: 'us'

  };

  weather: any;

  constructor(private weatherService: WeatherService) {

  }

  ngOnInit() {
    this.weatherService.getWeather(this.location.city, this.location.code).subscribe(result => {
      console.log(result);

      this.weather = result;
      // console.log(this.weather.city.name);
    });
  }

}
天气服务

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';

import { map } from 'rxjs/operators';

@Injectable({
  providedIn: 'root'
})
export class WeatherService {



  apiKey = 'Promise I didn't mess this part up';
  url;


  constructor(private http: HttpClient) {

    this.url = 'http://api.openweathermap.org/data/2.5/forecast?q=';
  }


  getWeather(city, code) {

    return this.http.get(this.url + city + ',' + code + '&APPID=' + this.apiKey).pipe(map(result => {
      console.log(result);
    }));
  }

}

您没有从服务中退回任何东西,您应该将其作为

getWeather(city, code) 
      return this.http.get(this.url + city + ',' + code + '&APPID=' + this.apiKey).pipe(map(result => {
          console.log(result); 
          return result;
        }));
 }

您忘记在控制台日志替换下面的方法之后返回result

getWeather(city, code) {

    return this.http.get(this.url + city + ',' + code + '&APPID=' + this.apiKey).pipe(map(result => {
      console.log(result);
      return result;
    }));
  }

希望这有帮助!:

getWeather应该在console.log之后返回结果?如果您使用的是RxJS映射操作符,则需要以某种格式返回映射数据,不管是什么格式。现在您没有返回map主体中的任何内容,这实际上不会向subscribe传递任何内容。在示例中,您甚至不需要管道或贴图。若真的需要,您可以只在subscribe中使用console.log。您需要在控制台登录后从getWeather返回结果,甚至不需要进行映射。在你的例子中这是不必要的。如果你需要记录一些东西,你可以使用一个副作用操作符,例如,如果你只想添加一个日志语句而不影响内容,你应该使用tap操作符而不是map。你是一个圣人,我知道那里有些可疑的东西!非常感谢,我会尽快接受答案D