Php Angular 2如何为HTTP请求定义基本URL

Php Angular 2如何为HTTP请求定义基本URL,php,laravel,angular,api,Php,Laravel,Angular,Api,我已经在我的应用程序中手动声明了http url。这对于本地环境是可以的,但当它准备部署到服务器上时,需要根据服务器主机进行更改。所以我需要一个解决方案来解决这个问题 提前谢谢 我知道有两种方法 创建一个env.js文件并编写以下代码 (function (window) { window.__env = window.__env || {}; // API url window.__env.baseUrl = 'http://localhost:8080'; //

我已经在我的应用程序中手动声明了http url。这对于本地环境是可以的,但当它准备部署到服务器上时,需要根据服务器主机进行更改。所以我需要一个解决方案来解决这个问题


提前谢谢

我知道有两种方法

创建一个env.js文件并编写以下代码

  (function (window) {

   window.__env = window.__env || {};

  // API url
  window.__env.baseUrl = 'http://localhost:8080';

  // Base url
   window.__env.middleware = '/api/v1';

  // Whether or not to enable debug mode
  // Setting this to false will disable console output
  window.__env.enableDebug = true;
}(this));
你用的是我喜欢的。它帮助我们管理头部并分离与API相关的代码。例如:

var axios = require('axios');
var axiosApi = axios.create({

    baseURL: config.host,

    headers: {

   // "authorization": "Basic dXNlckBjbG9uZWN0LmNvbTpQYXNzQDEyMw==",
      "content-type": "application/json"
    },
  // withCredentials: true,
      auth: {
        username: config.user,
        password: config.password
      }
   })
而config.host、config.user是根据您的环境类型在config.js中声明的变量