我正在尝试从休息数据服务调用中设置2个变量,这会在元素“_embedded”和“page”中返回JSON文档中的结果。
我试图在一个组件中有2个变量,观察服务中设置的2个变量。我所经历的是1个观察者被定义并包含结果而另一个没有,因为它在服务中由于某种原因而被“未定义”。
错误是当我尝试执行此语句时_pageObservier未定义this._pageObserver.next(this._page);
感谢任何见解......
@Injectable()
export class ProductService {
products$: Observable<Product[]>;
pager$: Observable<{}>;
private _baseUrl: string;
private _pageObserver: Observer<{}>;
private _productObserver: Observer<Product[]>;
private _page:{};
private _dataStore: { products: Product[] };
constructor(private _http: Http) {
this._baseUrl = 'http://localhost:8000/products';
this.products$ = new Observable(observer => this._productObserver = observer).share();
this.pager$ = new Observable(observer => this._pageObserver = observer).share();
this._dataStore = { products: [] };
this._page = {};
}
load() {
//this._http.get(`${this._baseUrl}/product`).map(response => response.json()).subscribe(data => {
this._http.get(`http://localhost:8000/products`).map(response => response.json()).subscribe(data => {
this._dataStore.products = data['_embedded'].products;
this._productObserver.next(this._dataStore.products);
this._page = data['page'];
this._pageObserver.next(this._page); <=== THIS IS NOT DEFINED
}, error => console.log('Could not load products.'));
}
答案 0 :(得分:0)
this._page = {};
this.pager$ = new Observable(observer => {
this._pageObserver = observer).share();
this._pageObserver.next(this._page);
});