如何在Angularjs拦截器中停止请求.
有没有办法做到这一点?
我尝试使用promises并发送拒绝而不是解决!
.factory('connectionInterceptor', ['$q', '$timeout',
function($q, $timeout) {
var connectionInterceptor = {
request: function(config) {
var q = $q.defer();
$timeout(function() {
q.reject();
}, 2000)
return q.promise;
// return config;
}
}
return connectionInterceptor;
}
])
.config(function($httpProvider) {
$httpProvider.interceptors.push('connectionInterceptor');
});
Run Code Online (Sandbox Code Playgroud) 我正在添加rxjs_compat到我的项目中以转移到v6库.
但是,现有HttpInterceptor的全局错误处理不再编译.不知道该去哪里.尝试各种各样.尝试所有的类型不匹配.
import { Injectable } from "@angular/core";
import {
HttpEvent,
HttpInterceptor,
HttpHandler,
HttpRequest,
HttpResponse,
HttpErrorResponse
} from "@angular/common/http";
import { Observable, of, empty } from "rxjs";
import { ToastrService } from "ngx-toastr";
import { environment } from "../../environments/environment";
import { catchError, map } from "rxjs/operators";
@Injectable()
export class HttpErrorInterceptor implements HttpInterceptor {
constructor(private toastr: ToastrService) {}
intercept(
request: HttpRequest<any>,
next: HttpHandler
): Observable<HttpEvent<any>> {
return next.handle(request).pipe(
catchError(err => of(HttpErrorResponse)),
map(err => {
let message: string; …Run Code Online (Sandbox Code Playgroud)