TypeError: _this. is not a function

TypeError: _this. is not a function

当我尝试从另一个服务函数调用的 subscribe() 方法中调用一个服务函数时,出现了一个奇怪的错误。 TypeError 指出:

TypeError: _this.fileloaderService.downloadFile is not a function

我有两项服务,一项用于获取剧集,另一项用于加载文件内容。我将它们都导入并将它们包含在我的 EpisodeComponent 构造函数中,如下所示:

import { EpisodeService } from '../episode.service';
import { FileloaderService } from '../fileloader.service';

constructor(private episodeService: EpisodeService,
  private fileloaderService: FileloaderService) { }

然后我得到我的剧集,当它们返回时,我尝试从 url 加载一个文件,这是我的剧集对象中的一个字段:

getEpisodes(): void {
  this.episodeService.getEpisodes().subscribe(episodes => {
    this.episodes = episodes;
    for (let i of this.episodes) {
      this.fileloaderService.downloadFile(i.fileUrl).subscribe(file => {
        i['file'] = file;
      });
    }
  });
}

然而,在控制台中 运行 它抱怨 downloadFile() 方法不存在。但是它编译正确,你可以看到它确实存在 在下面的服务中。

@Injectable()
export class FileloaderService {

  constructor(private http: HttpClient) { }

  downloadFile(fileUrl): Observable<Text> {
    let options = new RequestOptions({responseType: ResponseContentType.Text});
    return this.http.get(fileUrl, options)
      .catch(this.handleError('downloadFile'));
  }

它似乎在抱怨它没有 exist/isn 没有实例化什么的,但是我创建和包含这两个服务的方式是相同的。

有什么想法吗? (我是 Typescript 的新手,所以请放轻松!)提前致谢...

编辑 1:我正在 运行使用 ng serve --open 我已经包括:

console.log("fileloaderService:" + JSON.stringify(this.fileloaderService));

在 getEpisodes().subscribe() 函数中,它输出:

fileloaderService: {}

编辑 2:我还向 app.module.ts 提供商添加了服务,如下所示:

providers: [EpisodeService, FileloaderService],

编辑 3:我在此处创建了一个简化的应用程序堆栈闪电战:

https://stackblitz.com/edit/angular-sazw43

不幸的是,我正在努力让内存中的网络-api 的东西在此刻正常工作,但你应该能够看到我所拥有的一切(我认为)

嗨,你可以这样试试吗

getEpisodes(): void {
  this.episodeService.getEpisodes().subscribe(episodes => {
    this.episodes = episodes;
    var self = this;
    for (let i of this.episodes) {
      self.fileloaderService.downloadFile(i.fileUrl).subscribe(file => {
        i['file'] = file;
      });
    }
  });
}

这似乎是由 downloadFile() 方法中的 http.get() 格式不正确引起的,并且没有像我尝试使用的 RequestOptions 对象。

我认为这是由于将已弃用的@angular/http 库与较新的@angular/common/http

一起使用造成的

抱歉浪费大家的时间