Труба rxjs 6 не работает в Angular 6 - PullRequest
0 голосов
/ 24 июня 2018

У меня следующая проблема.В моем сервисе Angular6 есть запрос Http-Get.И я хочу добавить результат в мой магазин.Но tap -оператор в моем канале никогда не вызывается.

Когда я отлаживаю в chrome, я вижу, что мой http-запрос выполняется, но канал никогда не вызывается, что я делаю не так.

import { Injectable } from '@angular/core';
import { Budget, BudgetHistory } from '../../class/budget';
import { HttpClient } from '@angular/common/http';
import { PlatformLocation } from '@angular/common';
import { HistoryOfBudget } from '../../class/budget';
import { Observable } from 'rxjs';
import {ADD,LOAD,Store} from '../../class/store';
import { map, take, merge, switchMap, switchAll, tap, debounceTime } from 'rxjs/operators'

//Get access to localStorage
export const BudgetKey : string = "budget";

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

  private baseUrl : string;
  private http : HttpClient;
  private budgetStore : Store<Budget> = new Store<Budget>();
  private historyStore : Store<HistoryOfBudget> = new Store<HistoryOfBudget>();
  public buget$ : Observable<Budget>;
  public history$ : Observable<HistoryOfBudget>;

  constructor(platformLocation : PlatformLocation, http : HttpClient) {
    this.baseUrl = (platformLocation as any).location.origin;
    this.http = http;
    this.buget$ = this.budgetStore.items$;
    this.history$ = this.historyStore.items$;
   }


  public LoadHistory() : Observable<HistoryOfBudget>{
    return this.http.get<HistoryOfBudget>(this.baseUrl + '/api/history').pipe(
      tap((loadedHistory) => {
      let his = new HistoryOfBudget();
      his.budget = loadedHistory.budget;
      his.historyList = loadedHistory.historyList;
      this.historyStore.dispatch({type: LOAD, data: his});
    }));
  }

}

Таким образом, строка

let his = new HistoryOfBudget();

никогда не будет изменена в Chrome-отладчике.

Что я сделал не так?

Обновление:

Вот где я это называю.

 export class UebersichtComponent implements OnInit {

  private bs : BudgetserviceService;
  public history$: Observable<HistoryOfBudget>;

  constructor(bs : BudgetserviceService) { 
    this.bs = bs;
    this.history$ = this.bs.history$;
    this.bs.LoadHistory();
  }

  ngOnInit() {
  }

}

И здесь я подписался на Observable

<div *ngIf="(history$ | async) as history; else loading">
    <div *ngFor="let his of history.historyList">
      ....
    </div>
  </div>

Обновление 2:

Я тоже попробую это

public LoadHistory(){
this.history$ = this.http.get<HistoryOfBudget>(this.baseUrl + '/api/history').pipe(
  tap((loadedHistory) => {
  let his = new HistoryOfBudget();
  his.budget = loadedHistory.budget;
  his.historyList = loadedHistory.historyList;
  this.historyStore.dispatch({type: LOAD, data: his});
}));

}

1 Ответ

0 голосов
/ 24 июня 2018

Как уже упоминалось в комментариях, вы не подписаны на this.bs.LoadHistory()

constructor(bs : BudgetserviceService) { 
    this.bs = bs;
    this.history$ = this.bs.history$;
    this.bs.LoadHistory().subscribe(); // update this line
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...