Environment-based properties for Angular 2 App Served by Webpack?

后端 未结 2 1365
南旧
南旧 2021-01-15 05:00

I\'m working with a standalone Angular console generated by JHipster and served by a Spring Boot server. I am looking to serve up the app with different properties based on

2条回答
  •  深忆病人
    2021-01-15 05:48

    I recommend the dotenv-webpack plugin.

    webpack.config.js

    const  Dotenv  =  require('dotenv-webpack');
    ...
    plugins: [
       new  Dotenv({
          path:  './.env'
       })
    ]
    

    .env

    URL=http://example.com
    ENV=PROD
    ...
    

    This allows you to use process.env to access the environment variables:

    constructor(private http:HttpClient) {
         http.get(`${process.env.URL}`).subscribe(t=> {
            ...
         });
    }
    

    The environment variables are subsituted at build time.

提交回复
热议问题