I encountered an error while using angular2-interceptors in my Angular2 project. When attempting to call a POST/GET method, the following message appeared:
'Property 'post' does not exist on type 'ServerURLInterceptor'.'
I need guidance on where I might be making a mistake. My intention is to use interceptors to track every request, but whenever I try to make a request like GET or POST, it shows the error message saying that 'Property 'post' does not exist on type 'ServerURLInterceptor'.
Here is the code snippet:
The interceptorService.ts file:
import { Interceptor, InterceptedRequest, InterceptedResponse } from 'ng2-interceptors';
export class ServerURLInterceptor implements Interceptor {
public interceptBefore(request: InterceptedRequest): InterceptedRequest {
console.log(request);
return request;
}
public interceptAfter(response: InterceptedResponse): InterceptedResponse {
console.log(response);
return response;
}
}
The app.module.ts file:
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { MaterialModule } from '@angular/material';
import { AppComponent } from './app.component';
import { AppRoutingModule } from './app-routing.module';
import { LoginComponent } from './components/login/login.component';
import { FormsModule } from '@angular/forms';
import { LoginService } from './helper/services/login.service';
import { InterceptorService } from 'ng2-interceptors';
import { ServerURLInterceptor } from './helper/interceptor/interceptorService';
import { XHRBackend, RequestOptions } from '@angular/http';
export function interceptorFactory(xhrBackend: XHRBackend, requestOptions: RequestOptions, serverURLInterceptor:ServerURLInterceptor){
let service = new InterceptorService(xhrBackend, requestOptions);
service.addInterceptor(serverURLInterceptor);
console.log("interceptor");
return service;
}
@NgModule({
imports: [
MaterialModule.forRoot(),
BrowserModule,
AppRoutingModule,
FormsModule
],
declarations: [
AppComponent,
LoginComponent
],
providers: [
LoginService,
ServerURLInterceptor,
{
provide: InterceptorService,
useFactory: interceptorFactory,
deps: [XHRBackend, RequestOptions, ServerURLInterceptor]
}
],
bootstrap: [ AppComponent ]
})
export class AppModule { }
The login.service.ts file:
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { Http } from '@angular/http';
import { ServerURLInterceptor } from '../../helper/interceptor/interceptorService';
@Injectable()
export class LoginService {
constructor(private http:ServerURLInterceptor){}
login(username:string,password:string) {
console.log(username+" , "+password+" in LoginService");
debugger;
return this.http.post('http://localhost:4100/login',{
email: username,
password:password
});
}
}