未捕获的TypeError:无法在HttpXhrBackend读取未定义的属性'method'

问题描述 投票:0回答:2

我正在关注使用Angular HttpInterceptor的教程,但是当我调用handle()方法时遇到错误

//next.handle();

Uncaught TypeError: Cannot read property 'method' of undefined at HttpXhrBackend.push../node_modules/@angular/common/fesm5/http.js.HttpXhrBackend.handle (http://localhost:4200/vendor.js:31148:17) at eval (eval at push../src/app/services/authInterceptor.service.ts.AuthInterceptorService.intercept (http://localhost:4200/main.js:756:9), <anonymous>:1:6) at AuthInterceptorService.push../src/app/services/authInterceptor.service.ts.AuthInterceptorService.intercept (http://localhost:4200/main.js:756:9) at HttpInterceptorHandler.push../node_modules/@angular/common/fesm5/http.js.HttpInterceptorHandler.handle (http://localhost:4200/vendor.js:30859:33) at HttpXsrfInterceptor.push../node_modules/@angular/common/fesm5/http.js.HttpXsrfInterceptor.intercept (http://localhost:4200/vendor.js:31450:25) at HttpInterceptorHandler.push../node_modules/@angular/common/fesm5/http.js.HttpInterceptorHandler.handle (http://localhost:4200/vendor.js:30859:33) at HttpInterceptingHandler.push../node_modules/@angular/common/fesm5/http.js.HttpInterceptingHandler.handle (http://localhost:4200/vendor.js:31494:27) at MergeMapSubscriber.project (http://localhost:4200/vendor.js:30699:184) at MergeMapSubscriber.push../node_modules/rxjs/_esm5/internal/operators/mergeMap.js.MergeMapSubscriber._tryNext (http://localhost:4200/vendor.js:143493:27) at MergeMapSubscriber.push../node_modules/rxjs/_esm5/internal/operators/mergeMap.js.MergeMapSubscriber._next (http://localhost:4200/vendor.js:143483:18)


import { Injectable } from '@angular/core';
import { HttpInterceptor } from '@angular/common/http';

@Injectable()
export class AuthInterceptorService implements HttpInterceptor{
  intercept(req,next){
    console.log(req);
    return next.handle();
  }

}

有什么我做错了吗?因为请求被记录好了。

我使用的是Angular 7.0.3版

angular
2个回答
1
投票

请发送req句柄

intercept(req,next){
   console.log(req);
   return next.handle(req); <--- here
}

1
投票

试试这个

intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    console.log(req);
    return next.handle(req);
  }
© www.soinside.com 2019 - 2024. All rights reserved.