-角度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 { }
我可以看到intercept函数定义处的调试点被触发,但在完成一些服务调用后,intercept内部的代码并没有被调用。
我使用@angular/http中的angular-http调用服务,使用get、options和post方法