简体   繁体   English

如何在 Angular 中的生产构建期间设置 API 路径 15

[英]How to set API path during production build in Angular 15

In Angular 15, There is no environment files so we are using proxy.config file for setting api path for local development but that will not work in production build.在 Angular 15 中,没有环境文件,因此我们使用 proxy.config 文件为本地开发设置 api 路径,但这在生产构建中不起作用。

Below is the proxy.config.json file.下面是 proxy.config.json 文件。 在此处输入图像描述

This is my package.json file.这是我的 package.json 文件。 在此处输入图像描述

is anyone has suggestion for production build work with dynamic api path setting?有人建议使用动态 api 路径设置进行生产构建吗?

Environment files have been removed for convenience purposes: when a new user comes to the framework, this is less boilerplate code to deal with.为方便起见,环境文件已被删除:当新用户使用该框架时,需要处理的样板代码更少。

The same applies to karma conf, test config, etc.这同样适用于 karma conf、测试配置等。

That does not mean you can't add them back.这并不意味着您不能将它们添加回去。

But just in case, a new feature is this one:但以防万一,这是一个新功能:

import { isDevMode } from '@angular/core';

This states if you're in dev mode or prod mode.这表明您是处于开发模式还是生产模式。 You can work with that if all you need is a different URL.如果您只需要一个不同的 URL,您可以使用它。

you can set the API path for your production build by using the --base-href flag when you run the您可以在运行时使用 --base-href 标志为生产构建设置 API 路径

ng build

command.命令。 This flag specifies the base URL that will be used for the application, and it will be prepended to all the URLs used in the application.此标志指定将用于应用程序的基数 URL,并将添加到应用程序中使用的所有 URL 之前。 You can also set the baseHref property in the angular.json file under the build options for your project.您还可以在项目构建选项下的 angular.json 文件中设置 baseHref 属性。 For example:例如:

"build": {
  "options": {
    "baseHref": "https://api.example.com"
  }
}

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

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