[英]How to change variable value in index.html based on the environment in angular
How can i change config.apiKey value based on the angular environments like Development & Production.我如何根据开发和生产等角度环境更改 config.apiKey 值。
For Production config.appKey = 'AB-AAB-AAB-MPR';
For Development config.appKey = 'AC-DDR-SST-DKR';
Please find the code for reference.
`<script charset='UTF-8'>
window['adrum-start-time'] = new Date().getTime();
(function(config){
config.appKey = 'AC-DDR-SST-DKR';
})(window['adrum-config'] || (window['adrum-config'] = {}));
</script>
`
Thanks
Reading environment file in index.html will be difficult.读取 index.html 中的环境文件会很困难。 I am not saying it is not possible there may be a workaround for that.我并不是说不可能有解决方法。 But I recommend you to do this in app.component.ts file on但我建议您在 app.component.ts 文件中执行此操作
ngOnInit() {
this.loadScript();
}
private loadScript() {
const script = document.createElement('script');
script.type = 'text/javascript';
script.innerHTML = 'window["adrum-start-time"] = new Date().getTime(); (function(config){ config.appKey = ' + environment.key + '; })(window["adrum-config"] || (window["adrum-config"] = {}));';
const head = document.getElementsByTagName('head')[0];
if (head !== null && head !== undefined) {
document.head.appendChild(script);
}
}
Hopefully, this will solve the problem希望这能解决问题
You can use your angular.json to replace your index.html
file.您可以使用 angular.json 替换index.html
文件。 just create a copy of the index.html
and name it to index.env.html
and then in your angular.json
you can use fileReplacements
in your specific env like the following只需创建index.html
的副本并将其命名为index.env.html
,然后在您的angular.json
您可以在您的特定fileReplacements
中使用fileReplacements
,如下所示
"fileReplacements": [
{
"replace": "src/index.html",
"with": "src/index.env.html"
}
]
You can simply use isDevMode()
您可以简单地使用isDevMode()
or else use this way否则使用这种方式
└──myProject/src/environments/
└──environment.ts
└──environment.prod.ts
└──environment.stage.ts
export const environment = {
production: false,
apiUrl: 'http://my-api-url'
};
export const environment = {
production: true,
apiUrl: 'http://my-prod-url'
};
Read more on angular guide阅读有关角度指南的更多信息
you can add those key in environment.ts
and environment.prod.ts
file so you need to write the development api key in environment.ts
and production key in environment.prod.ts
so if you are creating a build by using a this command ng build --prod
it will take the api key from environment.prod.ts
file.您可以添加在这些关键environment.ts
和environment.prod.ts
所以你需要写在发展的API密钥文件environment.ts
在生产关键environment.prod.ts
所以如果你正在使用此命令创建构建ng build --prod
它将从environment.prod.ts
文件中获取 api 密钥。 here is the example这是例子
environment.ts环境.ts
export const environment = {
production: false,
appKey: 'AB-AAB-AAB-MPR';
};
environment.prod.ts环境.prod.ts
export const environment = {
production: true,
appKey: 'AC-DDR-SST-DKR';
};
service服务
// here we are importing our environment like this
import { environment } from '../../environments/environment';
getData() {
console.log(environment.apiKey);
// here you will get the apiKey based on your environment if it is dev environment then it take the "apiKey" from environment.ts and if it is production env then it will take the "apiKey" from the environment.prod.ts
// api call or other business logic
}
You can find solution here: How to use environment variable in index.html for Angular 6您可以在这里找到解决方案: How to use environment variable in index.html for Angular 6
TL,DR: use files for each enviroment and config it in angular.json TL,DR:为每个环境使用文件并在 angular.json 中进行配置
you can simply either use env files to manage your different environments or else you can also use this i have use in my react app as below in your package.json use你可以简单地使用 env 文件来管理你的不同环境,或者你也可以使用我在我的反应应用程序中使用的这个,如下你的 package.json 使用
"start": "react-scripts start",
"start:staging": "REACT_APP_BUILD_TYPE=staging npm start",
"start:prod": "REACT_APP_BUILD_TYPE=production npm start",
"build": "REACT_APP_BUILD_TYPE=production react-scripts build",
"build:staging": "REACT_APP_BUILD_TYPE=staging react-scripts build",
},
and the file you have written above can be use as你上面写的文件可以用作
"REACT_APP_BUILD_TYPE=staging ? config.appKey = 'AB-AAB-AAB-MPR' : config.appKey = 'AC-DDR-SST-DKR';
export const environment = {
production: true,
apiUrl: 'http://my-api-url',
appKey: 'AC-DDR-SST-DKR'
};
check the angular.json file for fileReplacements config here it is: stackblitz检查 angular.json 文件中的 fileReplacements 配置,它是: stackblitz
我为 prod、staging 等环境创建了不同的文件夹,而不是使用变量,我根据环境放置了三个不同的 index.html,我在这里回答了相同的问题https://stackoverflow.com/a/67091452/1843984
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.