Where to save global settings in Angular 4

后端 未结 4 2248
被撕碎了的回忆
被撕碎了的回忆 2021-02-19 05:45

In Angular (Type Script) there are many config files. Which is the right one to save a global setting?

For example, when I am calling API from local then my rootUr

4条回答
  •  梦毁少年i
    2021-02-19 06:29

    You can use process.env with webpack quite easily. E.g.

    /**
     * This interface makes sure we don't miss adding a property to both `prod` and `test`
     */
    interface Config {
      someItem: string;
    }
    
    /**
     * We only export a single thing. The config.
     */
    export let config: Config;
    
    /**
     * `process.env.NODE_ENV` definition is driven from webpack
     *
     * The whole `else` block will be removed in the emitted JavaScript
     *  for a production build
     */
    if (process.env.NODE_ENV === 'production') {
      config = {
        someItem: 'prod'
      }
      console.log('Running in prod');
    } else {
      config = {
        someItem: 'test'
      }
      console.log('Running in test');
    }
    

    and you can change process.env.NODE_ENV using webpack -p --define process.env.NODE_ENV='\"production\"' --config ./src/webpack.config.js

    More

    https://basarat.gitbooks.io/typescript/docs/tips/build-toggles.html

提交回复
热议问题