简体   繁体   English

Angular App找不到我的接口文件

[英]Angular App unable to find my interface file

Im trying to make a simple test bed for the OMBD Api. I have the following interface file called ombdresponse.ts:我正在尝试为 OMBD Api 制作一个简单的测试台。我有以下名为 ombdresponse.ts 的接口文件:

interface IOMBDResponse {
    Title: string;
    Year: string;
    Director: string;
    Poster: string;
}

The error I am having lies in my ombd-api.service.ts:我遇到的错误在于我的 ombd-api.service.ts:

import { Injectable } from '@angular/core';
import { HttpClient, HttpErrorResponse } from '@angular/common/http';
import { Observable } from 'rxjs';
import { catchError, tap } from 'rxjs';
import { throwError } from 'rxjs';

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

  private _siteURL="https://www.ombdapi.com/";
  private _key = '?apikey=63455f5a';


  constructor(private _http:HttpClient) { }

  getMovieData(movieName): Observable<IOMBDResponse> {
    return this._http.get<IOMBDResponse>(this._siteURL + this._key + movieName)
    .pipe(
      tap(data => console.log('Moviedata/error' + JSON.stringify(data))
      ),
      catchError(this.handleError)
    );
  }

  private handleError(err:HttpErrorResponse) {
    console.log('OmbdApiService: ' + err.message);
    return throwError(err.message);
  }


}

I also have the same error in my app.component.ts:我的 app.component.ts 中也有同样的错误:

import { Component } from '@angular/core';
import { Axios } from 'axios';
import { OmbdApiService } from './services/ombd-api.service';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css'],
 
})
export class AppComponent {
  title = 'openstackProject';

  movieData:IOMBDResponse;
  
 
  errorMessage: any;

  constructor(private _ombdService: OmbdApiService) {

  }

  getMovieDetails(movieName:string) : boolean {
    this._ombdService.getMovieData(movieName).subscribe(
      movieData => {
        this.movieData=movieData;
        console.log('Director name: ' + this.movieData.Director);
      },
      error => this.errorMessage = <any>error
    );
    return false;
  }

}

I get the following two errors:我收到以下两个错误:

Error: src/app/services/ombd-api.service.ts:19:27 - error TS2304: Cannot find name 'IOMBDResponse'.

19     return this._http.get<IOMBDResponse>(this._siteURL + this._key + movieName)
                             ~~~~~~~~~~~~~

and

Error: src/app/app.component.ts:14:13 - error TS2304: Cannot find name 'IOMBDResponse'.

14   movieData:IOMBDResponse;

So clearly the app is having an issue finding my Interface file.很明显,该应用程序在查找我的界面文件时遇到了问题。 However, the interface file comes up in intellisense.但是,接口文件出现在智能感知中。 Can someone explain what I am doing wrong?有人可以解释我做错了什么吗?

Tried making the interface public.尝试公开界面。 I am very new to using external APIs with angular.我对 angular 使用外部 API 还很陌生。

You need to export and to import your interface like this您需要像这样导出和导入您的界面

export interface IOMBDResponse {
    Title: string;
    Year: string;
    Director: string;
    Poster: string;
}

and where you need it import { IOMBDResponse } from '/directory/'以及你需要它的地方import { IOMBDResponse } from '/directory/'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM