Axios谷粒学院学习

1、概述

axios是独立的项目,不是vue里面的一部分,但是axios经常和vue一起使用,实现Ajax操作

2、应用场景

用于前端和后端的交互,使后端的数据在前端进行渲染。

3、axios使用

    第一步:创建html文件
    第二步:引入vue.min.js和axios.min.js的js包
    第三步:编写axios代码

axios+vue格式如下

    <script src="vue.min.js"></script>
    <script src="axios.min.js"></script>
    <script>
       new Vue({
           el: '#app',
            data: {//在data定义变量和初始值
                

           },
           created(){//页面渲染之前执行
            //调用定义的方法

           },
           methods:{//编写具体的方法

           }


      })
    </script>

创建Json文件,模拟查询的数据

{
“success”:true,
“code”:20000,
“message”:“成功”,
“data”:{
“items”:[
{“name”:“lucy”,“age”:20},
{“name”:“tom”,“age”:22},
{“name”:“jone”,“age”:25}
]
}
}

使用axios发送请求

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initialscale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>
    <div id="app">

    </div>
    <script src="vue.min.js"></script>
    <script src="axios.min.js"></script>
    <script>
       new Vue({
           el: '#app',
            data: {//在data定义变量和初始值
               //定义变量,
               userList:[] 

           },
           created(){//页面渲染之前执行
            //调用定义的方法
                this.getUserList()
           },
           methods:{//编写具体的方法
                //创建方法,查询所有用户信息
                getUserList(){
                    //使用axios发送ajax请求
                    //axios+提交方式+(请求接口的路径).then(箭头函数).catch(箭头函数)
                    axios.get("data.json")
                    .then(response =>{
                      //response就是请求之后返回的数据
                      console.log("----->"+response)
                    })//请求成功执行then方法
                    .catch(error =>{

                    })//请求失败执行catch方法
                }

           }


      })
    </script>
</body>
</html>

终极版本

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initialscale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>
    <div id="app">
      <!-- 把userList里面的数据先显示出来 -->
     <!-- <div v-for="user in userList">
         {{user.name}}-----{{user.age}}
     </div> -->
     <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: {//在data定义变量和初始值
               //定义变量,
               userList:[] 

           },
           created(){//页面渲染之前执行
            //调用定义的方法
                this.getUserList()
           },
           methods:{//编写具体的方法
                //创建方法,查询所有用户信息
                getUserList(){
                    //使用axios发送ajax请求
                    //axios+提交方式+(请求接口的路径).then(箭头函数).catch(箭头函数)
                    axios.get("data.json")
                    .then(response =>{
                      //response就是请求之后返回的数据
                      //通过response获取具体数据,赋值给定义的空数组
                      this.userList=response.data.data.items
                      console.log(this.userList)
                    })//请求成功执行then方法
                    .catch(error =>{

                    })//请求失败执行catch方法
                }

           }


      })
    </script>
</body>

</html>

测试结果
在这里插入图片描述
主要axios的格式:默写10遍

  <script>
       new Vue({
           el: '#app',
            data: {//在data定义变量和初始值
               //定义变量,
               userList:[] 

           },
           created(){//页面渲染之前执行
            //调用定义的方法
                this.getUserList()
           },
           methods:{//编写具体的方法
                //创建方法,查询所有用户信息
                getUserList(){
                    //使用axios发送ajax请求
                    //axios+提交方式+(请求接口的路径).then(箭头函数).catch(箭头函数)
                    axios.get("data.json")
                    .then(response =>{
                      //response就是请求之后返回的数据
                      //通过response获取具体数据,赋值给定义的空数组
                      this.userList=response.data.data.items
                      console.log(this.userList)
                    })//请求成功执行then方法
                    .catch(error =>{

                    })//请求失败执行catch方法
                }

           }


      })
    </script>
posted @ 2020-08-04 16:38  池三岁  阅读(189)  评论(0编辑  收藏  举报