文章内容
2020/6/13 9:40:53,作 者: 黄兵
Angular中的环境变量
是否需要根据您所处的环境使用不同的值?例如,如果您要构建与Stripe API对话的应用程序,则需要在开发过程中使用测试可发布密钥,然后在生产中使用实时可发布密钥。使用environment.ts
文件在Angular中很容易做到。
在生产环境中时,Angular CLI项目已经使用production
环境变量来启用生产模式:
// ... if (environment.production) { enableProdMode(); } // ...
您还将注意到,默认情况下,该/src/environment
文件夹中有一个用于开发的环境文件和一个用于生产的环境文件。假设我们想成为另一只动物,取决于我们处于开发或生产模式:
environment.ts
export const environment = { production: false, animal: '🐊' };
environment.prod.ts
export const environment = { production: true, animal: '🐔' };
在我们的组件中,我们要做的就是访问变量:
app.component.ts
import { Component } from '@angular/core'; import { environment } from '../environments/environment'; @Component({ ... }) export class AppComponent { animal: string = environment.animal; }
就这么简单!Angular负责将环境文件交换为正确的文件。
现在,在开发模式动物变量将会是:🐊,如果您运行ng build --prod,动物变量将会是:🐔。
javascript && and || 运算
UnicodeDecodeError: 'gbk' codec can't decode byte 0x99 in position 821: illegal multibyte sequence
UnicodeDecodeError: 'gbk' codec can't decode byte 0x99 in position 821: illegal multibyte sequence
评论列表