Angular(6)的错误TS2304:找不到名称“ ICurrentWeatherData”

时间:2018-12-28 08:41:50

标签: angular typescript rxjs karma-jasmine

使用Angular6时会发生错误,尽管我的应用程序可以运行并正常运行,但测试无法通过。这是文件的代码段:

`import { Injectable } from '@angular/core'
import { HttpClient } from '@angular/common/http'
import { Observable } from 'rxjs'
import { map } from 'rxjs/operators'
import { environment } from '../../environments/environment'
import { ICurrentWeather } from '../interfaces'

@Injectable()
export class WeatherService {
  constructor(private httpClient: HttpClient) {}

  getCurrentWeather(city: string, country: string): Observable<ICurrentWeather> {
    return this.httpClient
      .get<ICurrentWeatherData>(
        `${environment.baseUrl}api.openweathermap.org/data/2.5/weather?` +
          `q=${city},${country}&appid=${environment.appId}`
      )
      .pipe(map(data => this.transformToICurrentWeather(data)))
  }

  private transformToICurrentWeather(data: ICurrentWeatherData): ICurrentWeather {
    return {
      city: data.name,
      country: data.sys.country,
      date: data.dt * 1000,
      image: `http://openweathermap.org/img/w/${data.weather[0].icon}.png`,
      temperature: this.convertKelvinToFahrenheit(data.main.temp),
      description: data.weather[0].description,
    }
  }
  private convertKelvinToFahrenheit(kelvin: number): number {
    return (kelvin * 9) / 5 - 459.67
  }
}`
    src / app / weather / weather.service.ts(14,12)中的
  • ERROR:TS2304错误: 找不到名称“ ICurrentWeatherData”。

  • src / app / weather / weather.service.ts(21,44):错误TS2304:找不到 名称为“ ICurrentWeatherData”。

1 个答案:

答案 0 :(得分:0)

u应该导入该接口 我认为当您更换时这是可行的 这个:

import { ICurrentWeather } from '../interfaces'

至:

import { ICurrentWeather } from '../interfaces/ICurrentWeather'