【问题标题】:Problem with url after building an angular app构建 Angular 应用程序后 url 出现问题
【发布时间】:2021-06-03 06:39:00
【问题描述】:

我无法解决 URL 问题。当我在 localhost 上运行应用程序时,它工作正常,但是当我构建它并在测试服务器上运行它时,应用程序会尝试与 localhost 通信。

我有一个代码可以根据它是在本地服务器上还是在测试服务器上运行来切换 url。

这是我的代码:

baseUrl: string = this.isLocal ? 'http://localhost:8080/anypage' : '/anypage';

 isLocal() {
    return (window.location.host.indexOf("localhost") > -1)
  }

它应该工作,但它不工作。是我遗漏了什么还是有更简单的方法来解决这个问题?

【问题讨论】:

    标签: angular build


    【解决方案1】:

    你忘了调用你的函数:

      baseUrl: string = this.isLocal() ? 'http://localhost:8080/anypage' : '/anypage';
    
      isLocal() {
        return (window.location.host.indexOf("localhost") > -1)
      }
    

    恐怕听起来很无聊,但是如果您需要检测proddev 环境,那么更多«Angular way» 是this

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-06-12
      • 2020-06-09
      • 2017-03-15
      • 1970-01-01
      • 1970-01-01
      • 2020-03-05
      • 1970-01-01
      相关资源
      最近更新 更多