我是 Angular 的新手(使用 AngularJs 很多年了),我正在努力使用 Observables :( 我有这个方法:
filterProducts(categoryId: string, questions: Question[], organisationId: string): Observable<any> {
var formulas = this.formulaService.getFromSelectedAnswers(questions);
let shouldFilter = this.shouldFilterProducts(categoryId, questions, formulas);
if (!shouldFilter) return;
return this.filterOrScore(categoryId, organisationId, formulas).pipe(map(products => {
if (!products || !products.length) return;
this.products.length = 0;
this.products.push.apply(this.products, products);
this.questionService.filterQuestionsByProducts(products, questions);
this.questionService.updateSession(questions);
}));
}
线路if (!shouldFilter) return;
不对。我需要它返回一个 Observable 以便我的订阅工作。有谁知道这是怎么做到的吗?
如果您不关心订阅中的返回值,则可以返回空值。这将完成 observable。
import {empty} from 'rxjs';
filterProducts(categoryId: string, questions: Question[], organisationId: string): Observable<any> {
var formulas = this.formulaService.getFromSelectedAnswers(questions);
let shouldFilter = this.shouldFilterProducts(categoryId, questions, formulas);
if (!shouldFilter) return empty();
return this.filterOrScore(categoryId, organisationId, formulas).pipe(map(products => {
if (!products || !products.length) return empty();
this.products.length = 0;
this.products.push.apply(this.products, products);
this.questionService.filterQuestionsByProducts(products, questions);
this.questionService.updateSession(questions);
}));
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句