0%

VUE 定时请求接口数据

项目需求:每 5 分钟自动刷新表格数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<script>
export default {
data() {
return {
tableData: [], // 表格数据
timer: null // 定时器
}
},
// 每5分钟刷新一次
created () {
this.timer = setInterval(() =>{
this.getData()
},1000* 300)
},
methods:{
// 请求数据
getData () {
this.$axios.get('/api/test').then((res) =>{
if (data.statusCode == "800") {
this.tableData = data.returnObj.result;
} else {
console.log("请求数据失败!");
}
})
},
//清除定时器
beforeDestroy () {
clearInterval(this.timer);
this.timer=null;
}
}
</script>