Я создаю приложение AngularJS, используя классы ES6 с передачей трассировки на ES5 в формате AMD.
в моем модуле я импортирую класс перехватчика и зарегистрирую его как службу, а затем зарегистрирую эту службу с помощью $httpProvider.interceptors в module.config:
var commonModule = angular.module(moduleName, [constants.name]);
import authenticationInterceptor from './authentication/authentication.interceptor';
commonModule.service('authenticationInterceptor', authenticationInterceptor);
commonModule.config( $httpProvider => {
$httpProvider.interceptors.push('authenticationInterceptor');
});
Мой класс перехватчика вводит как $q, так и службы $window, сохраняет их в конструкторе для последующего использования. Я последовал этой части с отладчиком, и инъекция происходит правильно:
'use strict';
/*jshint esnext: true */
var authenticationInterceptor = class AuthenticationInterceptor {
/* ngInject */
constructor($q, $window) {
this.$q = $q;
this.$window = $window;
}
responseError(rejection) {
var authToken = rejection.config.headers.Authorization;
if (rejection.status === 401 && !authToken) {
let authentication_url = rejection.data.errors[0].data.authenticationUrl;
this.$window.location.replace(authentication_url);
return this.$q.defer(rejection);
}
return this.$q.reject(rejections);
}
}
authenticationInterceptor.$inject = ['$q', '$window'];
export default authenticationInterceptor;
Когда я делаю запрос, который отвечает 401, триггеры перехватчиков соответственно, но в методе "responseError" переменная 'this' указывает на объект окна, а не на мой перехватчик, поэтому я не имеют доступа к этому. $q или this. $window.
Я не могу понять, почему? Любые идеи?