Access environment variables in production build Angular 4

后端 未结 4 910
萌比男神i
萌比男神i 2020-11-28 15:07

I want to deploy a production build of angular app with a configurable api url for the user to test it out. I use the environment.ts but after the production build, I do not

4条回答
  •  天命终不由人
    2020-11-28 15:15

    Do we really need APP_INITIALIZER to load dynamic environment variables

    Well unless we need some async call to get them. Otherwise I would suggest this:

    env.js

    (function (window) {
      window._env = window._env || {};
      window._env.url= 'http://api-url.com';
    }());
    

    index.html

    
      
    
    
      
    
    

    Finally add it in angular.json

    "assets": [
                  "any/env.js",
    

    Now you can just read the window using a service in your app

提交回复
热议问题