如何在Angular中的运行时设置deployUrl [英] How do I set the deployUrl at runtime in Angular

查看:1850
本文介绍了如何在Angular中的运行时设置deployUrl的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Angular中,可以通过以下两种方式为项目定义" webpack_public_path "或" webpack_require .p":

In Angular the "webpack_public_path" or "webpack_require.p" can be defined for a project in a couple of ways:

  • 在.angular-cli.json文件中设置deployUrl
  • 在"ng build"命令行中添加--deployUrl"some/path"

但是,我需要能够在运行时设置此值,以便它在应用程序启动时来自我的assets/config.json文件.我正在考虑以下内容,可能是在main.ts文件中定义的,但是没有尝试过的方法:

However, I need to be able to set this value at runtime so it comes from my assets/config.json file when the applicaiton starts. I'm thinking of something like the following, perhaps defined in my main.ts file, but nothing I have tried works:

__webpack_require__.p=window.config['deployUrl'];

有人可以告诉我该怎么做,任何帮助将不胜感激!谢谢!

Could someone tell me how to do this, any help would be greatly appreciated! Thanks!

推荐答案

经过进一步调查,结果发现问题"

After some further investigation it turns out that the answer posted for the question "In Webpack, how do I set the public path dynamically?" did work. When I originally tried this I couldn't get it working, but after trying again I was successful.

唯一的区别是我没有创建globals.d.ts文件来添加clarify语句,而是将以下几行添加到了main.ts文件中:

The only difference was that I did not create a globals.d.ts file to add the declare statement, I just added the following lines into my main.ts file:

declare var  __webpack_public_path__:string;
__webpack_public_path__= 'public/path/location';

我不确定这是否是最佳做法...

I'm not sure if this is best practice or not...

这篇关于如何在Angular中的运行时设置deployUrl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆