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

问题描述 投票:1回答:2

使用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):错误TS2304:找不到名称'ICurrentWeatherData'。
  • src / app / weather / weather.service.ts(21,44):错误TS2304:找不到名称'ICurrentWeatherData'。
angular typescript rxjs karma-jasmine
2个回答
1
投票

你没有导入ICurrentWeatherData,在接口导入中我只看到ICurrentWeather


0
投票

当你替换它时,你应该导入我认为那个有效的接口:

import { ICurrentWeather } from '../interfaces'

至 :

import { ICurrentWeather } from '../interfaces/ICurrentWeather'
© www.soinside.com 2019 - 2024. All rights reserved.