简体   繁体   English

AngularJS:绝对路径与相对路径

[英]AngularJS: Absolute vs Relative Paths

I'm starting an AngularJS application and I need to call my static files with an absolute path in production mode and with a relative path in development. 我正在启动AngularJS应用程序,需要在生产模式下使用绝对路径调用静态文件,而在开发中则需要使用相对路径调用静态文件。

If for example I need to access a JS static folder, in production I'll need to call: https://statics.domain.com/js and in development I'll have a regular assets/js folder. 例如,如果我需要访问JS静态文件夹,则在生产中,我需要调用: https//statics.domain.com/js ,在开发中,我将有一个常规的assets/js文件夹。

Is there a best approach for that? 有没有最好的方法呢?

Thanks! 谢谢!

Edit: An important note is that this app is separated from the backend. 编辑:一个重要的注意事项是此应用程序与后端分离。

You can use the Base Tag ( https://developer.mozilla.org/en-US/docs/Web/HTML/Element/base ) to set the base path for all relative URLs. 您可以使用基本标记( https://developer.mozilla.org/en-US/docs/Web/HTML/Element/base )设置所有相对URL的基本路径。

So you could have in development and change that base href to static.domain.com in production. 因此,您可以进行开发并将生产环境中的基本href更改为static.domain.com。

However, the base tag is a bit obscure and I wouldn't recommend it for serious work. 但是,基本标记有点晦涩,我不建议您将其用于认真的工作。

Other ideas: 其他想法:

Could you use your hosts file to redirect static.testdomain.com to your local environment. 您能否使用主机文件将static.testdomain.com重定向到您的本地环境。

Could you use an environment variable or other configuration setting to alter the javascript references. 您能否使用环境变量或其他配置设置来更改javascript引用。

The best approach is to have your server serve different index.html files depending on the environment (production versus development). 最好的方法是让服务器根据环境(生产与开发)提供不同的index.html文件。 If you don't use a server at the moment you could consider a lightweight NodeJS one. 如果您目前不使用服务器,则可以考虑使用轻量级的NodeJS。

You could even use a templating engine (ex Express + Jade) and have the same index.html file be constructed differently based in the environment. 您甚至可以使用模板引擎(ex Express + Jade),并根据环境在不同的情况下构造相同的index.html文件。

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

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