简体   繁体   English

Angular 2 - angular-cli:在构建时设置全局变量

[英]Angular 2 - angular-cli: set global variable on build

This is my first time trying to deploy an Angular2 application. 这是我第一次尝试部署Angular2应用程序。 I want to set the URL of an API endpoint, and I would like for it to be different in every environment. 我想设置API端点的URL,我希望它在每个环境中都是不同的。

Is there a way to make ng build or any other tools that would allow set this for me prior to deployment? 有没有办法制作ng版本或任何其他工具,允许在部署之前为我设置?

With the Angular CLI tool you can configure different environments. 使用Angular CLI工具,您可以配置不同的环境。

ng build can specify both a build target ( --target=production or --target=development ) and an environment file to be used with that build ( --environment=dev or --environment=prod ). ng build可以指定构建目标( --target=production--target=development )和要与该构建一起使用的环境文件--environment=dev--environment=prod )。 By default, the development build target and environment are used. 默认情况下,使用开发构建目标和环境。

The mapping used to determine which environment file is used can be found in angular-cli.json: 用于确定使用哪个环境文件的映射可以在angular-cli.json中找到:

"apps": {
  "environmentSource": "environments/environment.ts",
  "environments": {
    "dev": "environments/environment.ts",
    "prod": "environments/environment.prod.ts"
  }
}

These options also apply to the serve command. 这些选项也适用于serve命令。 If you do not pass a value for environment, it will default to dev for development and prod for production. 如果不通过对环境的值,则默认为dev的发展和生产线进行生产。

You can also add your own env files other than dev and prod . 您还可以添加除devprod之外的自己的env文件。

Then, in your code, you can do a simple check, just like the default one in the main.ts file: 然后,在您的代码中,您可以进行简单的检查,就像main.ts文件中的默认检查一样:

if (environment.production) {
  enableProdMode();
}

Check out the build targets and environment files section in their docs for more details. 有关更多详细信息,请查看其文档中的构建目标和环境文件部分。

PS: Here's a similar question , you might find the answers there useful too. PS: 这是一个类似的问题 ,你可能会发现答案也很有用。

Yes, and it's actually quite convenient. 是的,它实际上非常方便。 Take a look at the src/environments/ directory. 看一下src/environments/目录。 You have there at least 2 files, one of which is environment.ts . 你有至少2个文件,其中一个是environment.ts Do not change this one . 不要改变这个 Instead you should put your different env-related configs in other files in this directory, eg environment.prod.ts . 相反,您应该将不同的env相关配置放在此目录中的其他文件中,例如environment.prod.ts

Everything is nicely explained in the comment put in src/environments/environment.ts : src/environments/environment.ts中的注释中很好地解释了所有内容:

The file contents for the current environment will overwrite these during build. 当前环境的文件内容将在构建期间覆盖这些内容。

The build system defaults to the dev environment which uses environment.ts , but if you do 构建系统默认使用environment.ts .ts environment.ts ,但是如果你这样做

ng build --env=prod then environment.prod.ts will be used instead. ng build --env=prod然后将使用environment.prod.ts

The list of which env maps to which file can be found in angular-cli.json . 可以在angular-cli.json找到哪个env映射到哪个文件angular-cli.json

So you just have to import the const defined in that file using a normal typescript import, eg 所以你只需要使用普通的typescript导入导入该文件中定义的const,例如

import { environment } from '../../environments/environment';

Then you can set it up in the service: 然后你可以在服务中进行设置:

@Injectable()
export class SettingsService {
    public environment = environment;
}

and now you inject use such service in other places, like any other service ;) 现在你注入使用这样的服务在其他地方,像任何其他服务;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM