पर बाध्यकारी खो देता है मैं एएमडी प्रारूप में ईएस 5 को पारदर्शी ट्रेलर के साथ ईएस 6 कक्षाओं का उपयोग करके बिल्डिंग और एंगुलरजेएस ऐप बना रहा हूं।angularjs http interceptor class (ES6) 'इस'
मेरी मॉड्यूल मेंमैं इंटरसेप्टर वर्ग आयात और एक सेवा के रूप में यह रजिस्टर, और फिर 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 injects और $ विंडो सेवाएं, उन्हें बाद में उपयोग के लिए निर्माता में सहेजती है। मैं डीबगर के इस हिस्से का पालन किया और इंजेक्शन ठीक से हो रहा है:
'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;
जब मैं एक अनुरोध है कि एक इंटरसेप्टर उचित रूप से चलाता है के साथ प्रतिक्रिया करते हैं, लेकिन 'responseError' विधि 'इस' चर में विंडो ऑब्जेक्ट को इंगित करता है, न कि मेरे इंटरसेप्टर को, इसलिए मुझे तक पहुंच नहीं है। $ q या यह $ विंडो।
मुझे पता नहीं लगा सकता क्यों? कोई विचार?
कि व्यावहारिक प्रतिक्रिया के लिए धन्यवाद। मुझे कोणीय स्रोत कोड में खुदाई पसंद नहीं आया, यह बहुत घना है। ईएस 6 के लिए, यह ईएस 5 का एक सुपरसेट है, इस प्रकार मैं शैलियों को मिश्रित कर सकता हूं जहां अभी आवश्यक है, जो मैंने किया है। मैंने इंटरसेप्टर क्लास को एक ऐसे फ़ंक्शन में बदल दिया जो ऑब्जेक्ट देता है, बाकी सब कुछ वही था और यह काम करता था। मैं वक्र से आगे निकलने के लिए ES6 के लिए दबाव डाल रहा हूं, लेकिन कभी-कभी जब आप अपने दांतों में बग प्राप्त करते हैं :-) –