当我们执行ng serve --env=prod
时,它不应该与environment.prod.ts中设置的值一起使用吗?好吧,在我的情况下,它没有:我总是得到environment.ts值! (如你所知,这是开发版)
我引入了所有相关部分,我认为在Angular 2+中使用环境变量时最重要。
如果我错过了,你能指出我吗?main.ts
import { enableProdMode } from '@angular/core';
import { environment } from './environments/environment';
if (environment.production) {
enableProdMode();
} // if I run enableProdMode();
environment.ts
export const environment = {
production: false,
serverBasePath: '',
};
environment.prod.ts
export const environment = {
production: true,
serverBasePath: '/mySubFolder',
};
component.ts
import { environment } from './../../../environments/environment';
...
this.serverBasePath = environment.serverBasePath;
最后,只是为了覆盖我们的基地......
角cli.json
...
"environmentSource": "environments/environment.ts",
"environments": {
"dev": "environments/environment.ts",
"prod": "environments/environment.prod.ts"
}
...
答案 0 :(得分:2)