Наблюдаемый не работает после обновления angular с angular 2.rc4 до final

#angular #rxjs #observable

#angular #rxjs #наблюдаемый

Вопрос:

После обновления моего приложения с angular 2.rc.4 и rxjs.beta.6 до angular 2 final и rxjs.beta.12 следующее просто больше не работает. Я не получаю сообщение об ошибке и ничего не могу найти в журналах изменений. Я вставил несколько комментариев в проблемный код. Все это отлично работало в rc.4

служба

 import { Injectable } from '@angular/core';
import { BehaviorSubject } from 'rxjs/BehaviorSubject';

import { ApiService } from './api.service';

@Injectable()
export class StocksService {

  private _portfolio: BehaviorSubject<any>;
  private _transactions: BehaviorSubject<any>;

  constructor(private apiService: ApiService) { 
    this._portfolio = BehaviorSubject.create();
  }

  get portfolio() {
    return this._portfolio.asObservable();
  }

  get transactions() {
    return this._transactions.asObservable();
  }

  loadPortfolio(): void {
    this.apiService.get('/depot')
    .map(res => res.json())
// the correct data is in res.data here, but it doesn't get to the component
    .subscribe(res => this._portfolio.next(res.data));
  }

  loadTransactions(): void {
    this.apiService.get('/transactions')
    .map(res => res.json())
    .subscribe(res => this._transactions.next(res.data));
  }

  search(id: string) {
    return this.apiService.get('/stocks/search')
    .map(res => res.json());
  }

}
 

и компонент

 import { Component, OnInit } from '@angular/core';
import { Observable } from 'rxjs/Observable';

import { StocksService } from './stocks.service';

@Component({
  selector: 'tradity-portfolio',
  templateUrl: 'app/portfolio.component.html',
  providers: [StocksService]
})
export class PortfolioComponent implements OnInit {

  portfolio: Observable<any>;

  constructor(private stocksService: StocksService) { }

  ngOnInit() {
    this.portfolio = this.stocksService.portfolio;
// the following console.log doesn't fire like it used to in rc.4
    this.portfolio.subscribe(val => console.log("received", val));
    this.stocksService.loadPortfolio();
  }

}
 

Комментарии:

1. Ваш код выглядит правильно, я думаю, проблема где-то в другом…

Ответ №1:

Я решил проблему сам. Ошибка была в конструкторе сервиса.

 constructor(private apiService: ApiService) { 
    this._portfolio = BehaviorSubject.create();
}
 

должно было быть

 constructor(private apiService: ApiService) { 
    this._portfolio = new BehaviorSubject("");
}