简体   繁体   English

JavaScript开发和生产模式

[英]JavaScript development and production mode

What is the best way to setup javaScript app settings for local (development) and production mode. 为本地(开发)和生产模式设置javaScript应用程序设置的最佳方法是什么。 Ex, I have different REST API links on my local machine and on the server. 例如,我在本地计算机和服务器上具有不同的REST API链接。 I can solve it by moving this setting into a different settings file and gitignore it. 我可以通过将此设置移到另一个设置文件中并对其进行gitignore来解决它。 It there any other possible solution for it? 还有其他解决方案吗? var debug mode = true? var debug mode = true? What is the best practice? 最佳做法是什么? Thanks! 谢谢!

As you've already noted, one option is simply to have: 正如您已经提到的,一种选择就是:

window.IS_DEBUG = true;

somewhere and then check: 某处,然后检查:

if(window.IS_DEBUG){ ...

Rather than hard-coding it though, another option would be to calculate it on load, by looking at the URL. 但是,与其对它进行硬编码,不如对它进行硬编码,还可以通过查看URL来在负载下进行计算。 For example: 例如:

$(function() {
    window.IS_DEBUG = window.location.indexOf('localhost') != -1;
    // Start the rest of your code
});

The advantage of this later approach is that you don't need to git-ignore any files (or have separate files at all). 此后一种方法的优点是您无需git-ignore任何文件(或根本没有单独的文件)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM