Hàm more()được cho là trả về một Observableyêu cầu nhận
export class Collection{
public more = (): Observable<Response> => {
if (this.hasMore()) {
return this.fetch();
}
else{
// return empty observable
}
}
private fetch = (): Observable<Response> => {
return this.http.get('some-url').map(
(res) => {
return res.json();
}
);
}
}
Trong trường hợp này tôi chỉ có thể thực hiện một yêu cầu nếu hasMore()là đúng, nếu không tôi gặp lỗi về subscribe()chức năng subscribe is not defined, làm thế nào tôi có thể trả về một sản phẩm nào có thể quan sát được?
this.collection.more().subscribe(
(res) =>{
console.log(res);
},
(err) =>{
console.log(err);
}
)
Cập nhật
Trong RXJS 6
import { EMPTY } from 'rxjs'
return EMPTY;
import "EmptyObservable" from "rxjs/observable/EmptyObservable";, sau đónew EmptyObservable<Response>();.