admin管理员组文章数量:1335158
In Java I usually create application.properties
in my resource folder and put configs in there.
And when I need it I just do Properties prop = new Properties(); prop.load(... my file)
and then use prop.getProperty("Something")
I want to do something similar in Javascript
In my js code I have this:
// REST API Base URL
var baseUrl = "http://localhost:8083/api";
I want this to be in a application.properties file and then load the value.
How can I achive this?
Thanks
In Java I usually create application.properties
in my resource folder and put configs in there.
And when I need it I just do Properties prop = new Properties(); prop.load(... my file)
and then use prop.getProperty("Something")
I want to do something similar in Javascript
In my js code I have this:
// REST API Base URL
var baseUrl = "http://localhost:8083/api";
I want this to be in a application.properties file and then load the value.
How can I achive this?
Thanks
Share Improve this question edited Nov 9, 2018 at 10:03 user10628262 asked Nov 9, 2018 at 9:59 user10628262user10628262 1031 silver badge8 bronze badges 3- 1 Create a file constants.js. Put every env var you wanna use in it and let it be the first file included by your app. – Batu.Khan Commented Nov 9, 2018 at 10:01
- Can you be more specific? I'm starting to have parkisson syndrome after doing a app in js – user10628262 Commented Nov 9, 2018 at 10:03
- Think of you have constants.java that you hold your global vars in it. Same consept. Except for js files, you need to include them into your html, right? So just be sure that that file is your very first file included in your html code. – Batu.Khan Commented Nov 9, 2018 at 10:05
2 Answers
Reset to default 3In angular 2+ projects and for a good practices you should create environments folder with one file per env like: environment.ts, environment.prod.ts.
and into file you can export a constant or by default like that
export const environment = {
apiUrl: '',
googleApiKey: '',
}
and you can import this environment in every file you will needed like
import { environment } from '{relativePath}/environment/environment.ts'
If you create different files for every env like prod. You need to replace environment.js for env that you will be build. You have lot of info about this with webpack or other pilers.
I remend you strongly to develop into a mon.js project. It will be more friendly for you importing modules and you will have powerful possibilities of scalable app.
But the easy(Ugly) solution is:
index.html
<head>
<script src="environment.js">
<script src="app.js">
</head>
environment.js
// Declaring environment like that you will have window scoped the variable
// and you will have global access to environment with window.environment
var environment = {apiUrl: 'https://api.url:4100'}
app.js
function example(){
console.log(window.environment.apiUrl); // out https://api.url:4100
}
The approach depends on how you build and/or bundle your AngularJs application. But regardless of that, you'll need to create a config.json file to contain your settings. If using browserify or webpack, you can import that file via require(...), otherwise you can simply request it via ajax before your app bootstraps.
In any of these cases, the best way to use the configuration data throughout your app is to define it as a constant at the bootstrap phase: app.constant('CONFIG', configData);
, assuming that configData is a variable that contains the data from your config.json file.
Then you can use dependency injection to provide CONFIG to all your controllers, services and directives.
本文标签: Properties file in JavaScriptAngularStack Overflow
版权声明:本文标题:Properties file in JavaScriptAngular - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742247312a2440093.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论