我正在跟踪有关使用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
答案 0 :(得分:1)
请发送req
句柄
intercept(req,next){
console.log(req);
return next.handle(req); <--- here
}
答案 1 :(得分:1)
尝试
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
console.log(req);
return next.handle(req);
}