我正在使用 angularjs,当我启动我的应用程序时,出现错误:
TypeError: Cannot read property 'push' of undefined
这是我的代码:
app.config([ '$routeProvider','$locationProvider', '$httpProvider', function($routeProvider, $httpProvider,$locationProvider) {
$httpProvider.interceptors.push(function ($q, $rootScope, $location) {
return {
'responseError': function(rejection) {
var status = rejection.status;
var config = rejection.config;
var method = config.method;
var url = config.url;
if (status == 401) {
$location.path( "/login" );
} else {
$rootScope.error = method + " on " + url + " failed with status " + status;
}
return $q.reject(rejection);
}
};
});
$httpProvider.interceptors.push(function ($q, $rootScope, $location) {
return {
'request': function(config) {
if (angular.isDefined($rootScope.authToken)) {
var authToken = $rootScope.authToken;
config.headers['X-Auth-Token'] = authToken;
}
return config || $q.when(config);
}
};
}
);
}]);
我不明白为什么会出现这个错误
最佳答案
您的注入(inject)顺序错误。您将 $locationProvider
放在数组中 $httpProvider
之前,因此您需要在参数中使用相同的顺序:
app.config([ '$routeProvider','$locationProvider', '$httpProvider',
function($routeProvider, $locationProvider, $httpProvider) {});
关于javascript - 如何解决 Angularjs TypeError : Cannot read property 'push' of undefined?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26811058/