[英]Rxjs combineLatest does returning the observable with in the suscribe
I am trying to get Userid and serviceId after I need to get a call based on the userid,serviceId problem here is it's not returning the observable to the ts.在我需要根据 userid 接听电话后,我正在尝试获取 Userid 和 serviceId,这里的 serviceId 问题是它没有将 observable 返回给 ts。
Service.ts服务.ts
function getData():observable<any>{
combineLatest([
this.activeSellService.getActiveSellServiceId(),
this.authService.getUserId(),
]).subscribe(([serviceId, userId]) => {
if (serviceId&& userId) {
const Url =
'users/' +
`${userId}` +
'/services/' +
`${serviceId}` +
'?value=test
return this.http.get<any>(this.endPoint.getUrl(encodeURI(Url)));
}
})
}
Component.ts:组件.ts:
this.service.getData().subscribe(data=>{console.log(data));
Even its not print the data in the console because the service is not returning observable.即使它没有在控制台中打印数据,因为服务没有返回可观察的。 kindly help me out with this problem.请帮我解决这个问题。 else can we go with different solutions in rxjs?否则我们可以在 rxjs 中使用不同的解决方案吗?
try this out试试这个
function getData():observable<any>{
return combineLatest(
this.activeSellService.getActiveSellServiceId(),
this.authService.getUserId(),
).pipe(mergeMap([serviceId, userId]) => {
if (serviceId && userId) {
const Url =
'users/' +
`${userId}` +
'/services/' +
`${serviceId}` +
'?value=test
return this.http.get<any>(this.endPoint.getUrl(encodeURI(Url)));
}
})
}
note on the arguments of combinelatest and there is not subscription in getData请注意 combinelatest 的参数,并且 getData 中没有订阅
eg in stackblitz:例如在堆栈闪电战中:
https://stackblitz.com/edit/angular-rxjs-combinelatest-hjyfa6?file=src/app/app.component.ts https://stackblitz.com/edit/angular-rxjs-combinelatest-hjyfa6?file=src/app/app.component.ts
You'd need to use a higher order mapping operator like switchMap
to map from one observable to another.您需要使用像switchMap
这样的高阶映射运算符来从一个可观察switchMap
映射到另一个。 Moreover, the subscribe()
function takes only callbacks and returns only the Subscription
object that contains the subscription.此外, subscribe()
函数只接受回调并且只返回包含订阅的Subscription
对象。
Also you aren't returning anything if the if
condition fails.如果if
条件失败,您也不会返回任何内容。 You could return an observable like RxJS constant EMPTY
that immediately completes the subscription if the condition fails.你可以返回一个像 RxJS 常量EMPTY
这样的 observable,如果条件失败,它会立即完成订阅。
Furthermore此外
observable<any>
must be Observable<any>
observable<any>
必须是Observable<any>
function
keyword isn't required in Typescript. Typescript 中不需要function
关键字。 Instead you could mention the scope identifier.相反,您可以提及范围标识符。combineLatest
.而不是combineLatest
提供的数据流。 In that case you could use forkJoin
instead.在这种情况下,您可以改用forkJoin
。 However note that forkJoin
only emits when all it's sources complete.但是请注意, forkJoin
仅在其所有源完成时才发出。Try the following尝试以下
import { Observable, forkJoin, EMPTY } from 'rxjs';
import { switchMap } from 'rxjs/operators';
public getData(): Observable<any> {
return forkJoin([
this.activeSellService.getActiveSellServiceId(),
this.authService.getUserId(),
]).pipe(
switchMap(([serviceId, userId]) => {
if (!!serviceId && !!userId) {
const Url = `users/${userId}/services/${serviceId}?value=test`;
return this.http.get<any>(this.endPoint.getUrl(encodeURI(Url)));
} else {
return EMPTY;
}
})
);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.