作者:小菜鸡 | 来源:互联网 | 2022-12-09 18:46
1> DeborahK..:
我尝试了这种Stackblitz所具有的基本概念,并成功了。它取消了剩余的操作。请参阅下面的链接。
https://stackblitz.com/edit/angular-4ctwsd?file=src%2Fapp%2Fapp.component.ts
我在您的代码和我的代码之间看到的区别是,我使用了throw
而不是throwError
(您写的是什么?),而我只是抛出错误……不返回抛出的错误。
这是供参考的代码:
import { Component } from '@angular/core';
import { of, from } from 'rxjs';
import { map, catchError, tap, retry} from 'rxjs/operators';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
name = 'Angular 6';
constructor() {
of('a', 'b', 'c', 'd')
.pipe(
map(x => {
if (x === 'c') {
throw 'An error has occurred';
}
return x;
}),
tap(x => console.log('In tap: ', x)),
retry(3),
catchError(() => of('caught error!'))
)
.subscribe(x => console.log(x));
}
}