Javascript 使用ES6时出现未知提供程序错误
我试图在Angular 1.4.x中创建一个简单的HTTP拦截器(使用ES6和Babel)。如果检测到HTTP 500,它只需要重定向到错误页面。守则: app/middleware/securityInterceptor.service.jsJavascript 使用ES6时出现未知提供程序错误,javascript,angularjs,Javascript,Angularjs,我试图在Angular 1.4.x中创建一个简单的HTTP拦截器(使用ES6和Babel)。如果检测到HTTP 500,它只需要重定向到错误页面。守则: app/middleware/securityInterceptor.service.js export default class SecurityInterceptorService { constructor($q, $location, config) { 'ngInject'; this.$location =
export default class SecurityInterceptorService {
constructor($q, $location, config) {
'ngInject';
this.$location = $location;
this.config = config;
this.$q = $q;
}
responseError(rejection) {
var deferred = this.$q.defer();
if (rejection.status === 500) {
this.$location.path(this.config.errorPagePath);
}
deferred.reject(rejection);
return deferred.promise;
}
}
import interceptorConfig from './index.interceptors';
import SecurityInterceptorService from '../app/middleware/securityInterceptor.service';
// other imports
angular.module('app', ['ngTouch' /*, other dependencies */])
.config(interceptorConfig)
.service('securityInterceptorService', SecurityInterceptorService)
// other declarations
export default function interceptorConfig($httpProvider, securityInterceptorService) {
'ngInject';
// Security interceptor
$httpProvider.interceptors.push(securityInterceptorService);
}
现在,我在尝试运行应用程序时收到一个未知提供程序错误,表示SecurityInterceptorService
未知。所以我看了一下它是如何注册的。但是,我没有看到任何明显的错误:
index.module.js
export default class SecurityInterceptorService {
constructor($q, $location, config) {
'ngInject';
this.$location = $location;
this.config = config;
this.$q = $q;
}
responseError(rejection) {
var deferred = this.$q.defer();
if (rejection.status === 500) {
this.$location.path(this.config.errorPagePath);
}
deferred.reject(rejection);
return deferred.promise;
}
}
import interceptorConfig from './index.interceptors';
import SecurityInterceptorService from '../app/middleware/securityInterceptor.service';
// other imports
angular.module('app', ['ngTouch' /*, other dependencies */])
.config(interceptorConfig)
.service('securityInterceptorService', SecurityInterceptorService)
// other declarations
export default function interceptorConfig($httpProvider, securityInterceptorService) {
'ngInject';
// Security interceptor
$httpProvider.interceptors.push(securityInterceptorService);
}
index.interceptors.js
export default class SecurityInterceptorService {
constructor($q, $location, config) {
'ngInject';
this.$location = $location;
this.config = config;
this.$q = $q;
}
responseError(rejection) {
var deferred = this.$q.defer();
if (rejection.status === 500) {
this.$location.path(this.config.errorPagePath);
}
deferred.reject(rejection);
return deferred.promise;
}
}
import interceptorConfig from './index.interceptors';
import SecurityInterceptorService from '../app/middleware/securityInterceptor.service';
// other imports
angular.module('app', ['ngTouch' /*, other dependencies */])
.config(interceptorConfig)
.service('securityInterceptorService', SecurityInterceptorService)
// other declarations
export default function interceptorConfig($httpProvider, securityInterceptorService) {
'ngInject';
// Security interceptor
$httpProvider.interceptors.push(securityInterceptorService);
}
删除securityInterceptorService
依赖项时,我可以进入interceptorConfig
函数。所以这项服务似乎没有注册。检查所有文件路径,检查变量名中的拼写错误,等等
如果有人能指出我犯的错误,我将不胜感激
------------------------更新
但是,通过.config()
注入的以下函数确实有效。然而,当我尝试在这个函数中推送securityInterceptor时,我得到了相同的未知提供程序错误
export default function config($logProvider, $locationProvider, toastr, $compileProvider) {
'ngInject';
// Enable log
$logProvider.debugEnabled(true);
// Set options third-party lib
toastr.options.timeOut = 3000;
toastr.options.positionClass = 'toast-top-right';
toastr.options.preventDuplicates = true;
toastr.options.progressBar = true;
// HTML 5 Modus Enabled No #
$locationProvider.html5Mode({
enabled: true,
requireBase: false,
hashPrefix: '!'
});
// Disable debug info
$compileProvider.debugInfoEnabled(true);
}
事实证明,您无法在
.config()
中插入服务。将index.interceptors.js
更改为以下内容时,一切正常:
export default function interceptorConfig($httpProvider) {
'ngInject';
// Security interceptor
$httpProvider.interceptors.push('securityInterceptorService');
}
function interceptorConfig($httpProvider,securityInterceptorService)
->securityInterceptorService
不是提供者,是吗?@zeroplagl它不是提供者。这就是我说的,这就是问题所在。您只能将提供程序注入配置函数。我用一些示例代码更新了我的帖子,这些代码确实有效,但也不是提供程序。它使用.config(config)
运行。每当我尝试向它添加securityInterceptor时,它也会失败。所以这个类有点问题,确切的错误是什么?我怀疑toastr
是否注册为服务。