常量在一个服务中未定义,但在另一个服务中有效

Constant is undefined in a service but works in another

本文关键字:服务 未定义 另一个 有效 一个 常量      更新时间:2023-09-26

我对angular有一个愚蠢的问题。我为我的应用定义了一个常量,我把它注入到两个不同的服务AuthProfile中。在Auth中,它就像一个咒语。在Profile中,它是未定义的。看不出我错过了什么。

auth.service.js:

(function() {
    'use strict';
    angular.module('App')
    .factory('AuthService', AuthService);
    AuthService.$inject = ['$http', '$q', '$window', 'API_URL'];
    function AuthService($http, $q, $window, API_URL) {
        //API_URL works
    }
})();

profile.service.js:

(function() {
    'use strict';
    angular.module('App')
    .factory('ProfileService', ProfileService);
    ProfileService.$inject = ['$http', '$q', 'API_URL'];
    function ProfileService($http, $q, $window, API_URL) {
        //API_URL is undefined
    }
})();

app.constants.js

(function() {
    'use strict';
    angular.module('App')
    .constant('API_URL','/api/v1/');
})();

谢谢你的帮助。

您的ProfileService函数没有窗口注入,要么删除它,要么添加它,

ProfileService.$inject = ['$http', '$q', '$window', 'API_URL'];
//                                       ^^^^^^^^^
function ProfileService($http, $q, $window, API_URL) {
}