【问题标题】:make condition on environment.prod.ts file在 environment.prod.ts 文件上创建条件
【发布时间】:2019-06-19 02:17:03
【问题描述】:

enter image description here

如何在下面的代码和environment.ts 文件中设置条件。

export const environment = {
  production: true,
  if(our condiion = "impdev.something.com"){
   API_url:'https://impdev.something.com/Angular',
  }
  if(our condiion = "dev.something.com"){
    API_url:'https://dev.something.com/Angular',
  }
  if(our condiion = "app.something.com"){
    API_url:'https://app.something.com/Angular',
  }

};

【问题讨论】:

  • 您不能在 environment.ts 文件中添加条件,但在服务中您可以创建决定环境的函数
  • 好的,@TheParam 但是最好的方法是什么。因为我有 3 个环境
  • 我已经添加了解决这个问题的最佳方法

标签: php node.js angular angular-ui-router node-modules


【解决方案1】:

这里可以使用三元运算符,

export const environment = {
  production: true,
  API_url: your_condition_here ? '' : '';
};

但是你会从哪里得到这里的条件呢?

【讨论】:

  • 这是我的问题如何在这里获得条件
  • 什么时候检查这些条件?意味着解释一下你的流程
  • 表示此页面可能出现的情况
【解决方案2】:

Main.ts

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

环境.ts

export const environment = {
  production: false,
  animal: '?'
};

环境.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;
}

【讨论】:

    【解决方案3】:

    idk 如果是你想要的,但是在angular.json ctrl + f 搜索fileReplacements,在这里你可以告诉Angular在不同的模式下用哪个环境文件夹文件替换..然后你可以访问environment.ts 中的相同变量虽然是您的整个应用程序,但它会根据您的服务或构建设置自动替换。

    如果不确定,只需复制粘贴包含fileReplacements 属性的整个“生产” JSON,并使用您创建的新 environment.ts 文件(如environment.dev.ts)更改replace 值,然后将所有变量从一个环境复制到另一个环境并更改值,并通过应用程序使用它们

    【讨论】:

      【解决方案4】:

      您将使用以下解决方案以其他方式实现此目的。

      environment.ts

      export const environment = {
        production: true,
        dev: { serviceUrl: 'https://dev.something.com' },
        stage: { serviceUrl: 'https://stage.something.com' },
        prod: { serviceUrl: 'https://prod.something.com' },
        local: { serviceUrl: 'http://localhost:8080/' },
      };
      

      NetworkService.ts

      export class NetowrkService {
      
        url: string;
        env: string
        constructor(private http: HttpClient) {
          this.env = this.setENV();
          this.url = environment[this.env].serviceUrl;
        }
      
        setENV() {
          if (window.location.hostname.indexOf("dev") != -1) {
            return "dev";
          } else if (window.location.hostname.indexOf("stage") != -1) {
            return "stage";
          } else if (window.location.hostname.indexOf("localhost") != -1) {
            return "local";
          } else {
            return "prod";
          }
        }
      
      
         // Call rest API
      }
      

      【讨论】:

      • 是的,它可以工作,但是,有没有可能的共同使用,因为在这个项目中我们添加了大约 10 -12 服务文件。
      • 创建一个父服务类并在所有 12 个服务中继承它。这就是我们正在做的事情。无法在对象中添加条件
      猜你喜欢
      • 2021-04-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-21
      • 2021-01-13
      • 1970-01-01
      • 2010-12-07
      • 1970-01-01
      相关资源
      最近更新 更多