我正在使用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');
});
Run Code Online (Sandbox Code Playgroud)
我的拦截器类注入$ 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', …Run Code Online (Sandbox Code Playgroud) 我正在使用[gulp-traceur] [1]在我的angularjs应用程序(1.x)中将es6编译为js.当我尝试编译for循环时,我收到错误:
ReferenceError: $traceurRuntime is not defined
Run Code Online (Sandbox Code Playgroud)
看起来我需要将$ traceurRuntime注入我的控制器或其他东西.谁能帮我吗?