【发布时间】:2021-09-24 12:45:12
【问题描述】:
我正在寻找一种从另一个 Observable 返回 Observable 的方法。我发现您可以使用 pipe 和 map 运算符来做到这一点,但它似乎对我不起作用。我做错了什么?
我正在使用 Angular 9.1.12 和 rxjs ~6.5.4。
示例: 服务1
import { Observable, of } from 'rxjs';
export class Service1 {
test(): Observable<string> {
console.log(1);
return of('hey');
}
}
服务2
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
export class Service2 {
constructor(private service1: Service1) {}
test(): Observable<string> {
return this.service1.test().pipe(
map((value: string) => {
console.log(2);
return value;
})
);
}
}
组件
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
export class Component implements OnInit {
constructor(private service2: Service2) {}
test(): Observable<void> {
return this.service2.test().pipe(
map((value: string) => {
console.log(3);
}));
}
}
}
控制台只会输出1。
【问题讨论】:
-
您在哪里以及如何订阅?
-
哦,天哪,我在组件调用中添加了 .subscribe(),它现在可以工作了,太好了。您可以发布答案,我会接受。谢谢。
标签: angular typescript rxjs pipe observable