优化不必要的请求

看看这个⬇️
观察数据如何变化

在这个页面上,我们有 3 个按钮,分别获取 3 组不同的异步回调数据。使用同一个容器来显示下面的数据。当快速点击按钮 2 和 3 时,按钮 2 的异步请求仍处于挂起状态,而按钮 3 的请求已发送。一旦按钮 2 的回调完成,按钮 2 的数据就会显示在下方。过一会儿,当按钮 3 的回调完成时,数据将替换为按钮 3 的相应数据。

优化方案 1

设置一个标志来指示需要显示哪一个按钮回调。

example
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.12/vue.min.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>
<title>Request Optimization Example</title>
<style>
.btns button {
margin-right: 10px;
}
.result {
margin-top: 20px;
}
</style>
</head>
<body>
<div id="app">
<div class="btns">
<button v-for="index in 3" :key="index" @click="getResult(index)">
{{ `获取结果${index}` }}
</button>
</div>
<div class="result">
<div v-show="loading">加载中</div>
<div v-show="!loading">{{ result }}</div>
</div>
</div>
<script>
var app = new Vue({
el: "#app",
data: {
loading: false,
result: "",
activeIndex: null,
},
methods: {
getResult(index) {
this.loading = true;
this.activeIndex = index;
setTimeout(() => {
if (index !== this.activeIndex) {
return;
}
this.result = `按钮${index}的异步回调结果`;
this.loading = false;
}, 1000);
},
},
});
</script>
</body>
</html>

displaying the correct callback data below

虽然此时已经可以显示正确的回调结果,但是点击按钮时仍然会产生不必要的异步请求。

优化方案 2

通过 Axios cancelToken 取消请求

通过 Axios cancelToken 取消请求

完整代码如下
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.12/vue.min.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>
<title>Request Optimization Example</title>
<style>
.btns button {
margin-right: 10px;
}
.result {
margin-top: 20px;
}
</style>
</head>
<body>
<div id="app">
<div class="btns">
<button v-for="index in 3" :key="index" @click="getResult(index)">
{{ `获取结果${index}` }}
</button>
</div>
<div class="result">
<div v-show="loading">加载中</div>
<div v-show="!loading">{{ result }}</div>
</div>
</div>
<script>
var app = new Vue({
el: "#app",
data: {
loading: false,
result: "",
cancelFetch: null,
},
methods: {
getResult(index) {
this.loading = true;
if (this.cancelFetch) {
this.cancelFetch();
this.cancelFetch = null;
}
axios
.post("https://api.vvhan.com/api/bing?type=json", null, {
cancelToken: new axios.CancelToken((c) => {
this.cancelFetch = c;
}),
})
.then((res) => {
this.result = `按钮${index}的异步回调结果`;
this.loading = false;
})
.catch(() => {});
},
},
});
</script>
</body>
</html>

另外,我们可以使用 Debounce 来避免触发不必要的回调。

就这样,希望这能有所帮助。