-1
我已经在我的应用程序中手动声明了http
url。对于本地环境可以,但是当它准备在服务器上部署时,它需要根据服务器主机进行更改。所以我需要一个解决方案来解决这个问题。Angular 2如何为http请求定义基本URL
在此先感谢!
我已经在我的应用程序中手动声明了http
url。对于本地环境可以,但是当它准备在服务器上部署时,它需要根据服务器主机进行更改。所以我需要一个解决方案来解决这个问题。Angular 2如何为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));
您使用axios我用喜欢。它可以帮助我们管理标题并分离我们的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声明的变量。