问题描述
我想拦截所有“删除” 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。