这个有效:
this.http.get('/doesntexist1')
.finally(() => console.log('finally1'))
.subscribe(() => { });
但这不是:
const obs = this.http.get('/doesntexist2');
obs.finally(() => console.log('finally2'))
obs.subscribe(() => { });
两个 URL 都会产生 404。
我同时运行了两者,但我只看到“finally1”显示在控制台中,知道为什么吗?
最佳答案
不同之处在于,在第二个示例中,.finally
与 .subscribe
不在同一个流中,因为您没有链接它们。
您的第一个示例创建了这个流:
get -> finally -> subscribe
你的第二个例子创建了两个分支:
get -> finally
get -> subscribe
finally 不会在没有订阅的情况下执行。
如果要构建流,则需要使用前一个运算符的结果来链接运算符。它不像就地运算符。
关于rxjs - Observable 最终没有被解雇,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47315384/