How do I set the deployUrl at runtime in Angular

2019-08-27 20:58发布

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

  • Set the deployUrl in the .angular-cli.json file
  • Add --deployUrl "some/path" to the "ng build" command line

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!

1条回答
我欲成王,谁敢阻挡
2楼-- · 2019-08-27 21:48

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.

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...

查看更多
登录 后发表回答