Vue3中如何使用异步请求示例详解
作者:Python研究所 时间:2024-04-27 16:04:56
1、前言
接上节,我们初步体验了layui-vue的用法。相比其他ui框架,layui-vue的数据结构显得不是非常友好,但是经过数据拼凑也是能够成功运行的。
今天我们就主要介绍下在实际开发中最常用到的前后端接 * 互。因为大多数时候前端为了高性能,对于后端接口的调用都会采用异步的方式。那该如何在vue3中使用异步请求渲染页面呢?
2、快速开始
2.1、思路
预期:前端急速响应、初始数据为空,异步接口响应后对页面数据表格进行响应式更新。
我们都知道vue的核心特性就是响应式,为了能够实现我们预期的效果,我们需要进行以下几步。
首先安装axios
封装axios
设计接口
在vue视图中将表格数据变量声明为响应式。初始化空值。
在vue视图中异步调用接口
将从后端获取到的数据push到响应式变量中。
2.2、安装&封装axios
安装axios
npm i axios --save
封装axios
在src
下新建request/svc.js
,并对axios进行封装
import axios from 'axios'
// 设置超时时间
axios.defaults.timeout = 5000
// 创建axios实例
const service = axios.create({
baseURL: 'http://localhost:8000', // 所有的请求地址前缀部分
timeout: 60000, // 请求超时时间毫秒
withCredentials: true, // 异步请求携带cookie
headers: {
// 设置后端需要的传参类型
'Content-Type': 'application/json',
'token': 'your token',
// 'X-Requested-With': 'XMLHttpRequest',
},
})
// 导出
export default service
如上,我们对axios进行简单的封装,已经能够实现统一URL、超时时间和请求头。
关于请求拦截等后续再进行讲解。
2.3、设计接口
首先在src
下新建apis/bugs.js
文件,然后设计一个方法(接口)去调用后端接口。
// 导入axios实例
import httpRequest from '@/request/svc'
// 获取BUG信息
export function apiGetBugs() {
return httpRequest({
url: 'bug3',
method: 'get',
params: {'dd': 'xxx'},
})
}
如上,我们在接口文件中调用封装的axios实例对后端的接口发起请求,并将请求相应对象返回。
2.4、设计视图
有了上面的基础,我们可以在vue的视图中直接导入bugs.js中的接口,然后在实例的onMounted阶段去异步调用接口,当接口返回后再去更新页面。
<template>
<!-- 上节的内容 -->
<lay-button type="primary">原始按钮</lay-button>
<lay-button>默认按钮</lay-button>
<lay-button type="normal">百搭按钮</lay-button>
<lay-button type="warm">暖色按钮</lay-button>
<lay-button type="danger">警告按钮</lay-button>
<hr>
<!-- 本节内容 -->
<lay-table :columns="columns1" :dataSource="dataSource1"></lay-table>
</template>
<script>
// 本节内容
import { onMounted,reactive } from 'vue'
import { apiGetBugs } from '@/apis/bugs'
export default {
setup() {
console.log('do something...')
// 声明表格数据变量未声明式
let dataSource1=reactive([])
// 挂载阶段异步调用接口
onMounted(async()=>{
let res = await apiGetBugs()
const ret = res.data
console.log(ret)
// 将接口返回的数据push到响应式变量中
dataSource1.push(...[{close:ret[0][0],fixing:ret[0][1],refused:ret[0][2],fixed:ret[0][3],reopen:ret[0][4]},])
})
// 表头
const columns1 = [
{
title:"关闭",
width:"200px",
key:"close"
},
{
title:"修复中",
width:"200px",
key:"fixing"
},
{
title:"拒绝",
width:"200px",
key:"refused"
},
{
title:"已修复",
width:"200px",
key:"fixed"
},
{
title:"重新打开",
width:"200px",
key:"reopen",
ellipsisTooltip: true
}
]
// 完整的layui-vue的表格数据结构
return {
columns1,
dataSource1
}
}
}
</script>
<style>
</style>
2.5、最终效果
以上,我们就完成了vue3的异步请求后端并完成页面渲染。
来源:https://juejin.cn/post/7108605278953668639
标签:vue3,异步,请求
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
JavaScript快速实现一个颜色选择器
2024-04-28 09:38:09
![](https://img.aspxhome.com/file/2023/1/132851_0s.jpg)
TIOBE编程语言排行榜前20的语言入门书籍推荐
2023-04-05 12:19:48
![](https://img.aspxhome.com/file/2023/0/131860_0s.jpg)
Python安装官方whl包和tar.gz包的方法(推荐)
2022-04-27 05:37:47
python中如何利用matplotlib画多个并列的柱状图
2022-04-14 12:38:42
![](https://img.aspxhome.com/file/2023/7/97337_0s.png)
Python SSL证书验证问题解决方案
2022-11-06 13:54:35
![](https://img.aspxhome.com/file/2023/6/118056_0s.png)
TensorFlow低版本代码自动升级为1.0版本
2023-08-12 02:02:24
解析python调用函数加括号和不加括号的区别
2023-11-30 17:29:50
Sql Server中的非聚集索引详细介
2024-01-26 15:57:56
![](https://img.aspxhome.com/file/2023/0/92790_0s.png)
Python中利用ItsDangerous快捷实现数据加密
2022-06-09 23:24:41
![](https://img.aspxhome.com/file/2023/3/67283_0s.jpg)
GDAL 矢量属性数据修改方式(python)
2021-01-30 20:53:28
![](https://img.aspxhome.com/file/2023/7/78537_0s.jpg)
Python+NumPy绘制常见曲线的方法详解
2022-01-11 06:14:43
![](https://img.aspxhome.com/file/2023/3/99233_0s.png)
详解python中的三种命令行模块(sys.argv,argparse,click)
2021-04-24 12:57:10
Django使用模板后无法找到静态资源文件问题解决
2021-09-01 09:49:25
React实现表格选取
2023-08-25 02:56:17
![](https://img.aspxhome.com/file/2023/2/56162_0s.png)
详解Go语言中用 os/exec 执行命令的五种方法
2024-05-28 15:21:51
我们需要的是怎样的分页?
2007-09-28 20:24:00
![](https://img.aspxhome.com/file/UploadPic/20079/28/2007928202636587s.gif)
简单介绍MySQL中的事务机制
2024-01-14 15:18:31
![](https://img.aspxhome.com/file/2023/8/114958_0s.jpg)
远程连接sql server 2000服务器的解决方案
2010-03-08 13:33:00
解决Vue警告Write operation failed:computed value is readonly
2024-04-09 10:49:25
javascript ImgBox透明遮罩层背景图片展示
2024-02-27 04:51:07