角度8:类型'T'不能分配给类型'HttpEvent <any>'

时间:2020-05-07 08:31:24

标签: angular angular8

我正在实现一个拦截器来全局处理任何错误,但是我在下面遇到了错误,我尝试了很多解决方案,但是没有任何效果 My Interceptor

interceptor.service.ts

import { Injectable } from '@angular/core';
import { HttpRequest, HttpHandler, HttpEvent, HttpInterceptor } from '@angular/common/http';
import { Observable, throwError } from 'rxjs';
import { catchError } from 'rxjs/operators';
import { AuthService } from '../../core/service/auth.service';

@Injectable()
export class ErrorInterceptor implements HttpInterceptor {
   constructor(private authenticationService: AuthService) { }

   intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
      return next.handle(request).pipe(catchError(err => {
    if (err.status === 401) {
     // auto logout if 401 response returned from api
     this.authenticationService.logOut();
     location.reload(true);
  }

    const error = err.error.message || err.statusText;
    return throwError(error);
  }))
 }
}

package.json

{
  "name": "client-app",
  "version": "0.0.0",
  "scripts": {
  "ng": "ng",
  "start": "ng serve",
  "build": "ng build",
  "test": "ng test",
  "lint": "ng lint",
  "e2e": "ng e2e"
 },
   "private": true,
   "dependencies": {
   "@angular/animations": "~8.2.11",
   "@angular/cdk": "^8.2.3",
   "@angular/common": "~8.2.11",
   "@angular/compiler": "~8.2.11",
   "@angular/core": "~8.2.11",
   "@angular/forms": "~8.2.11",
   "@angular/material": "^8.2.3",
   "@angular/platform-browser": "~8.2.11",
   "@angular/platform-browser-dynamic": "~8.2.11",
   "@angular/router": "~8.2.11",
   "@fortawesome/angular-fontawesome": "^0.6.1",
   "@fortawesome/fontawesome-svg-core": "^1.2.28",
   "@fortawesome/free-brands-svg-icons": "^5.13.0",
   "@fortawesome/free-regular-svg-icons": "^5.13.0",
   "@fortawesome/free-solid-svg-icons": "^5.13.0",
   "@ng-bootstrap/ng-bootstrap": "^5.3.0",
   "@sweetalert2/ngx-sweetalert2": "^7.3.0",
   "bootstrap": "^4.4.1",
   "datatables.net": "^1.10.20",
   "datatables.net-bs": "^1.10.20",
   "datatables.net-bs4": "^1.10.20",
   "datatables.net-dt": "^1.10.20",
   "hammerjs": "^2.0.8",
   "jquery": "^3.4.1",
   "ngx-bootstrap": "^5.5.0",
   "ngx-toastr": "^11.3.3",
   "rxjs": "~6.4.0",
   "sweetalert2": "^9.10.10",
   "tslib": "^1.10.0",
   "zone.js": "~0.9.1"
 },
   "devDependencies": {
   "@angular-devkit/build-angular": "~0.803.13",
   "@angular/cli": "~8.3.13",
   "@angular/compiler-cli": "~8.2.11",
   "@angular/language-service": "~8.2.11",
   "@types/node": "~8.9.4",
   "@types/jasmine": "~3.3.8",
   "@types/jasminewd2": "~2.0.3",
   "codelyzer": "^5.0.0",
   "jasmine-core": "~3.4.0",
   "jasmine-spec-reporter": "~4.2.1",
   "karma": "~4.1.0",
   "karma-chrome-launcher": "~2.2.0",
   "karma-coverage-istanbul-reporter": "~2.0.1",
   "karma-jasmine": "~2.0.1",
   "karma-jasmine-html-reporter": "^1.4.0",
   "protractor": "~5.4.0",
   "ts-node": "~7.0.0",
   "tslint": "~5.15.0",
   "typescript": "~3.5.3"
  }
}

PS:-我不确定这是否是与角度相关的问题或特定于我的环境的,因为我无法在stackblitz中复制此问题

1 个答案:

答案 0 :(得分:1)

我没有任何错误。无论如何,不​​要返回在catchError运算符中可观察到的错误,请尝试将return throwError(error);替换为throw error;