angular 10 : 在拦截器的 http 请求中从 observable 订阅后我的请求没有发送
angular 10 : my request is not send after subscribe from observable in http request in interceptor
我想拦截所有“删除”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
}
}
}
这应该可以解决。
您最终需要 return 一个可观察对象。
我想拦截所有“删除”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
}
}
}
这应该可以解决。 您最终需要 return 一个可观察对象。