我正在使用
- Angular 4.2.4
我正在尝试使用以下角度代码拦截服务
import {Injectable} from '@angular/core';
import {HttpEvent, HttpInterceptor, HttpHandler, HttpRequest} from
'@angular/common/http';
import { Observable } from 'rxjs/Rx';
export class LocalHttpClientInterceptor implements HttpInterceptor {
intercept(req: HttpRequest<any>, next: HttpHandler):
Observable<HttpEvent<any>> {
const username = 'me';
const password = 'blahblah';
const authReq = req.clone({headers: req.headers.set('Authorization',
'Basic ' + btoa(username + ':' + password))});
return next.handle(authReq);
}
}
在模块文件中
import { LocalHttpClientInterceptor } from './local-http-client-
interceptor';
@NgModule({
imports: [
CommonModule,
FormsModule,
ReactiveFormsModule,
MdAutocompleteModule,
MdInputModule,
MdIconModule,
HttpModule,
MdGridListModule,
FlexLayoutModule
],
declarations: [ UserSearchComponent, ObjectComponent],
exports: [UserSearchComponent, ObjectComponent, FlexLayoutModule],
providers : [{
provide: HTTP_INTERCEPTORS,
useClass: LocalHttpClientInterceptor,
multi: true,
}, UserSearchService, LocalHttpClientService, StorageService]
})
export class SharedModule { }
问题:我能够看到拦截函数定义处的调试点被触发,但是在拦截内部执行一些服务调用代码之后没有被调用。
我正在使用来自 @angular/http 的 angular http 来使用 get、options、post 方法调用服务