简单使用
# 介绍
- axios是独立于vue的一个项目
 - 用于浏览器和node.js中发送ajax请求
 - 示例链接
 
# 示例
- 引入 axios.min.js 和 vue.min.js 文件
- 文件可以从 本示例中链接中获取
 
 
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <div id="app">
        <table>
            <tr v-for="user in userList">
                <td>{{user.name}}</td>
                <td>{{user.age}}</td>
            </tr>
        </table>
    </div>
    <script src="vue.min.js"></script>
    <script src="axios.min.js"></script>
    <script>
        new Vue({
            el: '#app',
            data: {
                userList:[]
            },
            created() { //在页面渲染之前执行
                //调用方法,得到返回json数据
                this.getList()
            },
            methods:{
                getList() {
                    //使用axios方式ajax请求
                    axios.get("user.json")
                        .then(response => {//请求成功
                            //console.log(response)
                            this.userList =  response.data.data.items
                            console.log(this.userList)
                        }) 
                        .catch(error => {
                            console.log(error)
                        }) //请求失败
                }
            }
        })
    </script>    
</body>
</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
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
- 使用的user.json数据如下
 
{
    "code": 200,
    "message": "success",
    "data": {
        "items": [
            {
                "name": "stt",
                "age": 22
            },
            {
                "name": "stt2",
                "age": 23
            }
        ]
    }
}
 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Last Updated: 2022/01/16, 11:29:51