首页 > 解决方案 > Is it possible to read env variable in Angular application

问题描述

I have an environment.ts file in which I want to store if the project was built in test mode or dev mode. At the moment, I store the value as a key-value pair

export const environment = {
...
testMode:false
...
}

Is it possible that I provide the value (false or true) from a build file (say package.json) or read from an environment variable?

标签: angular6

解决方案


您将拥有您的环境文件,

环境.ts:

export const environment = {  
  testMode: false,
  KEY_TO_READ: 'test'
};

它已导出,因此您可以导入它:

import { environment } from './environment';

export class MyappAppComponent {  
  title = 'myapp works!';
  KEY_TO_READ = environment.KEY_TO_READ;
}

推荐阅读