我有一個這個 Observable
obs1$ = this.otherService.getObservable() 這是型別號
我想創建一個使用此 Observable 的回傳值發出 Observable 的方法
combined() {
this.currentLambdaReactorState$.subscribe((e) => {
let dto = of([
{
name: 'foo',
value: e,
},
])
return dto
});
}
到目前為止,這個方法沒有回傳任何東西,我怎樣才能從那個方法回傳一個 Observable ?
uj5u.com熱心網友回復:
地圖就是你想要的。
// replace any with your type (returned from currentLambdaReactorState$)
combined(): Observable<{name: string, value: any}> {
return this.currentLambdaReactorState$.pipe(
// shorthand object literal return
map((e)=> ({name: 'foo', value: e}))
)
}
// usage
combined().subscribe((mappedValue) => {
console.log(mappedValue); // {name: 'foo', value: e}
});
uj5u.com熱心網友回復:
您可以在 this.currentLambdaReactorState$ 之前添加 return 并用 map 替換 subscribe
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/419644.html
標籤:
