在uniapp中使用axios出现Adapter 'http' is not available in the build错误
时间: 2024-03-08 21:47:14 浏览: 28
这个错误通常是因为你的代码在浏览器环境中运行,而浏览器环境中不支持使用Node.js的http模块。解决方法是使用浏览器可用的XMLHttpRequest或fetch API来替换http模块。
在Uniapp中使用axios,可以在main.js中添加以下代码:
```javascript
import axios from 'axios'
axios.defaults.adapter = function(config) {
return new Promise((resolve, reject) => {
const { url, method = 'get', data, headers } = config
uni.request({
url,
method,
data,
header: headers,
complete: function(response) {
resolve(response)
}
})
})
}
Vue.prototype.$http = axios
```
这样就可以使用axios来发起请求了,它会自动使用Uniapp的请求方式。
相关问题
uniapp项目中引入axios后报错Adapter 'http' is not available in the build怎么解决
这个问题可能是因为您在没有安装必要的包的情况下使用了 http 请求。请确保您已经在项目中正确配置了 axios 并安装了必要的包。如果您使用的是 uniapp,您可以尝试使用 uni.request() 来发送请求,uniapp 已经内置了该方法并支持发送 http 和 https 请求。
adapter 'http' is not available in the build
这个错误通常在使用`http`模块的时候出现。这个模块在最新版本的Node.js中已经被废弃,所以它不再是默认安装的模块。如果你想使用`http`模块,你需要手动安装它。
你可以使用以下命令安装它:
```
npm install http --save
```
如果你只是想使用Node.js自带的网络模块,可以使用`net`模块代替`http`模块。如果你需要更高级的网络功能,建议使用第三方库,比如`axios`或`request`。