私は ES6 クラスを使用して AngularJS アプリを構築しており、トレーサーは AMD 形式で ES5 にトランスパイルしています。
私のモジュールでは、インターセプター クラスをインポートしてサービスとして登録し、このサービスを module.config の $httpProvider.interceptors に登録します。
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」変数がインターセプターではなくウィンドウ オブジェクトを指しているため、this.$qまたはthis.$window .
理由がわかりませんか?何か案は?