在Angular 8 component中找不到返回对象数组的方法

在Angular 8 component中找不到返回对象数组的方法,angular,typescript,angular8,Angular,Typescript,Angular8,我是8号角的新手。我正在服务中创建一个方法,该方法允许我返回动态构造的数据结构 import { HttpClient } from '@angular/common/http'; import { Injectable } from '@angular/core'; import { AppConfig } from 'src/app/app.config'; import { map } from 'rxjs/operators'; @Injectable({ providedIn:

我是8号角的新手。我正在服务中创建一个方法,该方法允许我返回动态构造的数据结构

import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { AppConfig } from 'src/app/app.config';
import { map } from 'rxjs/operators';

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

  private editionUrls = AppConfig.evtSettings.files.editionUrls || [];
  private bibliographicCitations: Array<BibliographicCitation> = [];

  constructor(
    private http: HttpClient,
  ) {
  }

  public getBibliographicCitations() {
    const parser = new DOMParser();
    this.editionUrls.forEach((path) => {
      this.http.get(path, { responseType: 'text' }).pipe(map((response: string) => {
        Array.from(parser.parseFromString(response, 'text/xml').getElementsByTagName('bibl')).forEach(citation => {
          if (citation.getElementsByTagName('author').length === 0 &&
              citation.getElementsByTagName('title').length === 0 &&
              citation.getElementsByTagName('date').length === 0) {
            const interfacedCitation: BibliographicCitation = {
              title: citation.textContent.replace(/\s+/g, ' '),
            };
            if (!this.bibliographicCitations.includes(interfacedCitation)) { this.bibliographicCitations.push(interfacedCitation); }
          } else {
            const interfacedCitation: BibliographicCitation = {
              author: citation.getElementsByTagName('author'),
              title: String(citation.getElementsByTagName('title')[0]).replace(/\s+/g, ' '),
              date: citation.getElementsByTagName('date')[0],
            };
            if (!this.bibliographicCitations.includes(interfacedCitation)) { this.bibliographicCitations.push(interfacedCitation); }
          }
        });
        this.bibliographicCitations.forEach(biblCit => {
          console.log(((biblCit.author === undefined) ? '' : biblCit.author),
                      ((biblCit.title === undefined) ? '' : biblCit.title),
                      ((biblCit.date === undefined) ? '' : biblCit.date));
        });
      }),
      );
    });
    // THIS IS RETURNED EMPTY IN THE COMPONENT WHEN ACTUALLY IT IS FULL!
    return this.bibliographicCitations;
  }
}

export interface BibliographicCitation {
  author?: HTMLCollectionOf<Element>;
  title: string;
  date?: Element;
}

我想知道是否有一种方法可以通过避免立即订阅服务来返回数据。

我们在这里要做的是使用普通的javascript
map
函数返回可观察到的http调用流

public getBibliographicCitations() {
  return this.editionUrls.map((path) =>  this.http.get(path, { responseType: 'text' 
  }));
}
然后,为了得到这些值,我们必须订阅它,因为观测值总是。要订阅,我们可以执行以下操作:

import { forkJoin } from 'rxjs';

forkJoin(this.getBibliographicCitations()).subscribe(console.log);
这里我使用的是等待所有api调用的。一旦一切成功,您将能够在控制台中看到数据

无论您需要映射或处理从响应中获得的值,您都应该在subscribe函数中执行以下操作

forkJoin(this.getBibliographicCitations()).subscribe((responses) => {
  // Update instance variables accordingly
  this.bibliographicCitations = //;
});

谢谢

我们在这里要做的就是使用普通的javascript
map
函数返回一个可观察到的http调用流

public getBibliographicCitations() {
  return this.editionUrls.map((path) =>  this.http.get(path, { responseType: 'text' 
  }));
}
然后,为了得到这些值,我们必须订阅它,因为观测值总是。要订阅,我们可以执行以下操作:

import { forkJoin } from 'rxjs';

forkJoin(this.getBibliographicCitations()).subscribe(console.log);
这里我使用的是等待所有api调用的。一旦一切成功,您将能够在控制台中看到数据

无论您需要映射或处理从响应中获得的值,您都应该在subscribe函数中执行以下操作

forkJoin(this.getBibliographicCitations()).subscribe((responses) => {
  // Update instance variables accordingly
  this.bibliographicCitations = //;
});

谢谢

http。get
是异步的。您应该返回并订阅此可观察的返回表单。这是否回答了您的问题@利亚姆:我同意。但是,在我的例子中,我处于一个循环中。如果我不在,我会
返回这个.http.get(路径,{responseType:'text'}).pipe(map((response:string)=>{[…]
然后在组件中订阅。我希望的是,Angular仍然允许在一个
http
调用周期后返回数据。您的
http.get
是异步的。您应该返回并订阅从该表返回的可观察数据。这是否回答了您的问题?@Liam我同意。但是,在我的示例中,我处于一个循环中。如果我不在,我会执行
返回this.http.get(path,{responseType:'text'}).pipe(map((response:string)=>{[…]
然后在组件中订阅。我希望Angular在一个
http
调用周期后仍然允许返回数据。对不起,我的意思是将URL映射到http可观察对象。forEach不返回任何内容。我没有要求更改管道(map())。你能再看一遍答案吗?不,我的错,我想你是对的,我误解了OPs代码。我添加了
这个设置。书目引用
,如果这样我就可以改变我的投票。但这似乎是正确的
forkJoin(this.gethttpcallsobstream())。订阅((回复)=>{
在下一行
responses.forEach(response=>{
并从这些块中返回更新的数据结构,它似乎工作得很好!
forkJoin
正是我要找的,太好了!PS:
this.gethttpcallsobstream()
return
this.editionurl.map((path)=>this.http.get(path),{responseType:'text'}))
很高兴它对您有所帮助。很抱歉,我的意思是将URL映射到http可观察对象。forEach不返回任何内容。我没有要求更改管道(map())。你能再看一遍答案吗?不,我的错,我想你是对的,我误解了OPs代码。我添加了
这个设置。书目引用
,如果这样我就可以改变我的投票。但这似乎是正确的
forkJoin(this.gethttpcallsobstream())。订阅((回复)=>{
在下一行
responses.forEach(response=>{
并从这些块中返回更新的数据结构,它似乎工作得很好!
forkJoin
正是我要找的,太好了!PS:
this.gethttpcallsobstream()
return
this.editionurl.map((path)=>this.http.get(path),{responseType:'text'}))
很高兴它对您有所帮助。