Here is an example of my Angular TypeScript Interceptor:
export module httpMock_interceptor {
export class Interceptor {
static $inject: string[] = ['$q'];
constructor(public $q: ng.IQService) {}
public request(config: any) {
console.log(this);
}
}
}
In this module, I am registering the interceptor as a service.
import {httpMock_interceptor as interceptor} from './httpMock.interceptor';
var httpMock: ng.IModule = angular.module("httpMockTs", []);
httpMock.service("httpMockInterceptor",interceptor.Interceptor);
httpMock.config.$inject = ['$httpProvider'];
httpMock.config(['$httpProvider', function ($httpProvider: ng.IHttpProvider) {
$httpProvider.interceptors.unshift('httpMockInterceptor');
}]);
After initializing, the interceptor constructor sets the $q service. However, when it reaches the request method and uses the this keyword, the browser throws an error saying that this is undefined. Can anyone point out where I might be making a mistake?
And here is the transpiled code for the interceptor:
export var httpMock_interceptor;
(function (httpMock_interceptor) {
class Interceptor {
constructor(_q) {
this._q = _q;
}
request(config) {
console.log(this);
}
}
Interceptor.$inject = ['$q'];
httpMock_interceptor.Interceptor = Interceptor;
})(httpMock_interceptor || (httpMock_interceptor = {}));
This is the module:
import { httpMock_interceptor as interceptor } from './httpMock.interceptor';
var httpMock = angular.module("httpMockTs", []);
httpMock.service("httpMockInterceptor", interceptor.Interceptor);
httpMock.config.$inject = ['$httpProvider'];
httpMock.config(['$httpProvider', function ($httpProvider) {
$httpProvider.interceptors.unshift('httpMockInterceptor');
}]);
export { httpMock };