简体   繁体   English

试图将服务中的数据导入component.ts

[英]Trying to get data from service into component.ts

I am trying to just get data from the service and put it into my component.ts file. 我试图从服务中获取数据并将其放入我的component.ts文件中。 I have the service subscribe to the http get and it returns the data but after i try to get the data in the component. 我有服务订阅http get并返回数据,但在我尝试获取组件中的数据后。 I just don't know how to go about it not trying to get the data until its done doing the call in the service. 我只是不知道该如何去做它直到完成服务中的调用之后才尝试获取数据。

I'm not sure if I'm just doing the pipe wrong or if i should be subscribing. 我不确定我是在做管道错误还是我应该订阅。 Nothing I've looked up is really helping. 我没有看到任何东西真的有帮助。

In the console I get the pipe error and then after the error it outputs the observable from the service. 在控制台中我得到管道错误,然后在错误之后它从服务输出observable。

My SearchService.ts 我的SearchService.ts

import { Injectable } from '@angular/core';
import { HttpClient } from "@angular/common/http";
import { Row } from "../../app/row";
import { Observable } from "rxjs";
import { of } from "rxjs";

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

  players: Observable<Row[]>;

  constructor(private http: HttpClient) { }

  getPlayers(searchTerm: String): Observable<Row[]>{
    let ROOT_URL =
    "http://lookup-service-prod.mlb.com/json/named.search_player_all.bam?sport_code='mlb'&active_sw='Y'&name_part='" +
   searchTerm +
    "%25'";
    this.http.get<Row[]>(ROOT_URL).subscribe(data => {
      let res = data["search_player_all"];
      let query = res["queryResults"];
      let row = query["row"];
      if (row != null) {
        this.players = row;
        console.log(this.players);
        return this.players;
      }
    });
    return this.players;
  }
}

AppComponent.ts AppComponent.ts

import { Component } from "@angular/core";
import { Row } from "../app/row";
import { Observable } from "rxjs";
import { map } from 'rxjs/operators';
import { SearchServiceService } from "../app/services/search-service.service"

@Component({
  selector: "app-root",
  templateUrl: "./app.component.html",
  styleUrls: ["./app.component.scss"]
})
export class AppComponent {
  players: Row[];
  value = "";
  buttonClicked = false;
  numReturned = -1;
  searchTerm = "";
  constructor(private searchService: SearchServiceService) {}

  getData() {
    if (this.value != "") {
      this.searchTerm = this.value;

      this.searchService.getPlayers(this.searchTerm).pipe(map(data => {
        if(data == null) {
          console.log('IS NULL');
        }
        else {
          console.log('in service');
          this.players = data;
          console.log(this.players);
          this.buttonClicked = true;
        }
      }));

      if(this.players == null) {
        console.log('null');
        this.numReturned = -1;
      }

    }
  }

Try to return just the http.get like this: 尝试像这样返回http.get:

getPlayers(searchTerm: String): Observable<Row[]>{
    return this.http.get<Row[]>(ROOT_URL);
}

Also in your code this.players is not an Observable it is a plain data that your api returned. 同样在你的代码中this.players不是一个Observable,它是你的api返回的普通数据。

So instead of this: 所以不是这样的:

this.searchService.getPlayers(this.searchTerm).pipe(map(

Just subscribe: 订阅:

this.searchService.getPlayers(this.searchTerm).subscribe((data: any) => { <process your data here> });

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

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