Angular 8:为 api base url 设置生产环境

sa7*_*oun 2 api development-environment angular

在 angular 8 项目上工作,我想设置两种环境配置,一种用于 dev ,另一种用于 prod :

开发

export const environment = {
  production: false,
  baseUrl: 'http://localhost:8000/api'
};
Run Code Online (Sandbox Code Playgroud)

产品

export const environment = {
  production: true,
  baseUrl: 'https://serverurl/api/api'
};
Run Code Online (Sandbox Code Playgroud)

这里的问题是,当我运行npm run ng build --prod并测试之后,我的 api 上的注册路由不起作用,另一方面,当我用邮递员测试时它起作用!

abn*_*317 5

使用配置选项构建合适的环境

ng build --prod --configuration=production
Run Code Online (Sandbox Code Playgroud)

在你的angular.json你应该找到这样的东西:

"architect": {
  "build": {
    "configurations": {
      "production": {
        "fileReplacements": [
          {
            "replace": "src/environments/environment.ts",
            "with": "src/environments/environment.prod.ts"
          }
        ],
...
Run Code Online (Sandbox Code Playgroud)