美文网首页
五、axios详解

五、axios详解

作者: 让你变好的过程从来都不会很舒服 | 来源:发表于2022-10-13 17:51 被阅读0次

    1、axios组件

    Vue并不能直接支持ajax,所以通过组件来实现ajax异步请求。

    最为推荐的组件就是 axios。

    axios在线cnd

    <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
    

    示例:

    <!DOCTYPE html>
    <html lang="en" xmlns:v-bind="http://www.w3.org/1999/xhtml" xmlns:v-on="http://www.w3.org/1999/xhtml">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
        <!--1、导入Vue.js-->
        <script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
        <!--2、导入axios-->
        <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
    
        <style>
            /* 解决闪烁问题 */
            [v-clock]{
                display: none;
            }
        </style>
    
    </head>
    <body>
    
    <div id="app" v-clock>
        <div>{{info.name}}</div>
        <a v-bind:href="info.url">点我</a>
    </div>
    
    
    <script>
    
        var vm = new Vue({
            el:"#app",
            data(){
                return{
                    info:{
                        name: null,
                        url: null,
                        address: {
                            street: null,
                            city: null,
                            country: null
                        }
                    }
                }
            },
            mounted(){
                axios.get("../data.json").then(response=>(this.info=response.data));
            }
        });
    
    </script>
    
    </body>
    </html>
    

    data.json

    {
      "name":"狂神说java",
      "url": "http://baidu.com",
      "page": "1",
      "isNonProfit":"true",
      "address": {
        "street": "含光门",
        "city":"陕西西安",
        "country": "中国"
      },
      "links": [
        {
          "name": "B站",
          "url": "https://www.bilibili.com/"
        },
        {
          "name": "4399",
          "url": "https://www.4399.com/"
        },
        {
          "name": "百度",
          "url": "https://www.baidu.com/"
        }
      ]
    }
    

    结果:


    相关文章

      网友评论

          本文标题:五、axios详解

          本文链接:https://www.haomeiwen.com/subject/bceczrtx.html