-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaxios.html
72 lines (64 loc) · 1.89 KB
/
axios.html
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
63
64
65
66
67
68
69
70
71
72
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>axios use</title>
</head>
<body>
<div id="app"></div>
<script type="text/javascript" src="js/vue.js"></script>
<script type="text/javascript" src="js/vue-router.js"></script>
<script type="text/javascript" src="js/axios.js"></script>
<script>
var App={
template: `
<div>
<button @click="sendAjax">AAAAAAjax</button>
response1: {{ res1 }}
response2: {{ res2 }}
</div>
` ,
data: function (){
return {
res1: '',
res2: ''
}
},
methods: {
sendAjax: function () {
axios.get('http://localhost:3000/getdata',{
params: {
}
}).then(function (res) {
console.log(res);
}).catch(function (err) {
console.log(err);
})
},
sendSomeAjax: function (){
var q1=this.$axios.get('url1');
var q2=this.$axios.post('url2');
//合并请求
this.$axios.all([q1,q2])
.then(this.$axios.spread((res1,res2)=> {
// 此处使用箭头函数是为了自动继承this对象
this.res1=res1;
this.res2=res2;
}))
.catch(err=>{
console.log(err);
})
}
}
};
Vue.prototype.$axios=axios;
new Vue({
el: '#app',
components: {
app: App
},
template: '<app/>'
});
</script>
</body>
</html>