uniapp如何封装请求vue3
时间: 2023-11-11 07:04:32 浏览: 132
在uniapp中封装请求可以使用axios或者uni.request。
以下是使用axios封装请求的示例:
1. 安装axios
```
npm install axios --save
```
2. 创建request.js文件
```
import axios from 'axios'
const request = axios.create({
baseURL: 'http://localhost:3000',
timeout: 5000
})
request.interceptors.request.use(config => {
// 拦截请求,可以在这里添加token等信息
return config
}, error => {
return Promise.reject(error)
})
request.interceptors.response.use(response => {
// 拦截响应,可以在这里统一处理错误信息等
return response.data
}, error => {
return Promise.reject(error)
})
export default request
```
3. 发送请求
```
import request from './request'
export function login(params) {
return request.post('/login', params)
}
```
以上是使用axios封装请求的示例,使用uni.request也可以实现类似的封装。
阅读全文
相关推荐
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![js](https://img-home.csdnimg.cn/images/20250102104920.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)