如何解决角度10:我的请求在拦截器中的http请求中可观察到的订阅之后未发送
我想拦截所有“删除” http,并在之前进行确认对话框。 如果用户单击“是”,则发送请求,否则不附加任何内容。
我的拦截器看起来像:
@Injectable()
export class DeleteInterceptor implements HttpInterceptor {
constructor(private dialog: MatDialog) {
}
intercept(req: HttpRequest<any>,next: HttpHandler): Observable<HttpEvent<any>> {
if (req.method == 'DELETE') {
this.dialog.open(ConfirmDeleteComponent).afterClosed().subscribe(result => {
if (result) {
return next.handle(req); // don't send
}
});
}else{
return next.handle(req); // work correctly
}
}
}
感谢您的帮助, 伊娃
解决方法
@Injectable()
export class MyHttpInterceptor implements HttpInterceptor {
constructor(private dialog: MatDialog) {
}
intercept(req: HttpRequest<any>,next: HttpHandler): Observable<HttpEvent<any>> {
// add custom header
if (req.method == 'DELETE') {
console.log(req)
return this.dialog.open(ExampleDialogComponent).afterClosed().pipe(
filter(res => res),switchMap(res => next.handle(req) // don't send
)
)
}else{
return next.handle(req); // work correctly
}
}
}
这应该解决它。 您需要最后返回一个observable。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。