我正在努力对我们拥有的项目进行一些小修改,我认为这是基于AngularJS的。目前,我们手动更改API端点以指向不同的环境。我正在改变我们的gulpfile以使用gulp-ng-constant,以便我们可以根据build命令交换环境。这很容易。
该项目设置为使用config.js文件(不是config.json)所以我必须使用gulp-ng-constant写入新文件(configConst.json - > configConst.js) 。这里也没问题。
我似乎无法工作的是将新的configConst文件中的常量注入到现有的config.js文件中。
以下是已编译的configConst.js文件的内容:
"use strict";
angular.module("apiConstants", [])
.constant("baseEndPoint", "https://testserver.com");
这是完整的config.js(我将在下面解释):
'use strict';
angular.module('shell')
.constant('Config', {
viewsDir: 'views/',
componentsDir: 'components/',
statesDir: 'states/',
API: {
protocol: window.location.protocol.split(':')[0],
//Use the same protocol, host and port as the UI is hosted from bu default
host: window.location.hostname,
path: ''
},
getEnvironmentUrl: function(apiConstants) {
console.log(apiConstants);
var url = window.environmentVariables && window.environmentVariables.getUrl() || '';
window.environment = window.environmentVariables && window.environmentVariables.getEnvironment();
return url.indexOf('http') == -1 ? 'http://blah' :
url;
},
getKKServiceUrl: function() {
var url = window.environmentVariables && window.environmentVariables.getKKUrl();
return (!url || url.indexOf('{') != -1) ? '/kk' : url;
},
landingPageIsActive: function() {
var isActive = window.environmentVariables && window.environmentVariables.getLanding();
return isActive == 'true';
}
}).config(['$httpProvider', function($httpProvider) {
var checkVersion = function(version) {
//force browser refresh if api version has been changed
if (version && window.buildNumber && window.buildNumber[0] != '#' && !version.startsWith(window.buildNumber)) {
window.location.reload();
}
};
$httpProvider.interceptors.push(['$q', function($q) {
return {
responseError: function(rejection) {
checkVersion(rejection.headers('AssemblyVersion'));
return $q.reject(rejection);
},
response: function(response) {
checkVersion(response.headers('AssemblyVersion'));
var hash = response.headers('PermissionsHash');
if (hash) {
//debugger;
if (window.permissionsHash && window.permissionsHash != hash) {
//TODO: update permission service to work with new roles / permissions without refresh
//console.log(response.headers('PermissionsList'));
//window.location.reload();
}
if (!window.permissionsHash) {
window.permissionsHash = hash;
}
}
return response;
}
};
}]);
}])
.config(['componentFactoryProvider', '$resourceProvider', function(componentFactoryProvider, $resourceProvider) {
componentFactoryProvider.setViewPath(function(componentSnakeName) {
return 'components/' + componentSnakeName + '/' + componentSnakeName + '.html';
});
$resourceProvider.defaults.stripTrailingSlashes = false;
}]).value('cgBusyTemplateName', 'views/angular-busy/default-spinner.html').factory('BaseUrl', function(Config) {
return Config.API.protocol + '://' + Config.API.host + ':' + Config.API.port + '/';
}).factory('APIBaseUrl', ['apiConstants', 'Config', function(apiConstants, Config) {
return Config.getEnvironmentUrl(apiConstants);
}]).factory('KKUrl', ['Config', function(Config) {
return Config.getKKServiceUrl();
}]).config(['$breadcrumbProvider', function($breadcrumbProvider) {
$breadcrumbProvider.setOptions({
templateUrl: 'components/bread-crumbs/bread-crumb-template.html',
includeAbstract: true
});
}]).config([
'cfpLoadingBarProvider',
function(cfpLoadingBarProvider) {
cfpLoadingBarProvider.includeBar = false;
}
])
.config(['$authProvider', '$injector', 'APIBaseUrlProvider', '$httpProvider', 'permissionServiceProvider', 'LanguageProvider', 'Config', 'growlProvider', 'PermissionProvider',
function($authProvider, $injector, APIBaseUrlProvider, $httpProvider, permissionServiceProvider, LanguageProvider, Config, growlProvider, PermissionProvider) {
var permissionService = permissionServiceProvider.$get(),
apiUrl = APIBaseUrlProvider.$get(),
permissionProvider = PermissionProvider;
var injector = angular.injector(['ng']),
_q = injector.get('$q');
permissionProvider.defineDefaultValidationFunction(function(e, role) {
var deferred = _q.defer();
permissionService.hasOneRoleDefer(role).then(function(result) {
if (result) {
deferred.resolve();
} else {
deferred.reject();
}
});
return deferred.promise;
});
$authProvider.configure({
apiUrl: apiUrl,
emailSignInPath: Config.getKKServiceUrl() + '/Auth',
emailRegistrationPath: Config.getKKServiceUrl() + '/user/add',
storage: 'localStorage',
passwordResetPath: Config.getKKServiceUrl() + '/user/forgot',
handleLoginResponse: function(resp, status, headers) {
permissionService.setSSID(headers('ss-id'));
permissionService.setPID(headers('ss-pid'));
$httpProvider.defaults.headers.common.Authorization = resp.user.baseAuthHeader;
permissionService.setUser(resp.user);
permissionService.setHomeBreadCrumb();
var LanguageFactory = LanguageProvider.$get();
LanguageFactory.setCurrent((resp.user.language || '').toLowerCase());
return resp.data;
}
});
$httpProvider.interceptors.push(function() {
return {
request: function(config) {
if (config.url.startsWith(apiUrl)) {
config.url += '?ss-id=' + permissionService.getSSID() + '&ss-pid=' + permissionService.getPID();
}
return config;
}
};
});
$httpProvider.defaults.withCredentials = true;
growlProvider.globalDisableIcons(true);
growlProvider.globalDisableCountDown(true);
growlProvider.onlyUniqueMessages(false);
growlProvider.globalTimeToLive({ success: 5000, error: 5000, warning: 5000, info: 5000 });
}
])
.config(['uiMask.ConfigProvider', function(uiMaskConfigProvider) {
uiMaskConfigProvider.clearOnBlur(false);
uiMaskConfigProvider.allowInvalidValue(true);
}]);
我想将apiConstants注入APIBaseUrl的工厂。该工厂调用Config.getEnvironmentUrl函数。我想将apiConstants传递给该函数,然后我可以使用它来构建最终的API端点URL(当前http://blah)。
目前,当使用包含的代码提供应用程序时,console.log(apiConstants)行返回'undefined'。如果我将其更改为在angular.module ...行中注入[apiConstants],则浏览器会抱怨componentFactoryProvider是未知的提供者。我还以为我可以将apiConstants直接注入到Config常量中,但是我尝试过没有任何更好的进展。在Angular中并不精通并且不知道要搜索的正确术语,我不确定我是否采取了正确的方法。如何将configConst.js文件中的常量注入config.js文件?