欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Vue篇——Axios异步通信

程序员文章站 2022-07-12 19:20:13
...

上代码


Demo.html

<!--
@Author: 流浪者
@Description:
@Time: 2020/12/21 15:08
@Version: 1.0
-->


<!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>Vue_Demo3</title>
    <style>
        [v-clock] {
            display: none;
        }
    </style>
</head>
<body>

<div id="vue" v-clock>
    
    <div>{{info.name}}</div>

</div>


<script src="https://cdn.bootcss.com/vue/2.5.2/vue.min.js"></script>
<script src="https://cdn.bootcss.com/vue-router/3.0.1/vue-router.min.js"></script>
<script src="https://cdn.bootcss.com/vuex/3.0.1/vuex.min.js"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>

<script>

    var vm = new Vue({
        el: "#vue",
        //data:属性 vm
        data() {
            return {
                //请求返回参数,必须与json字符串一致
                info: {
                    name: null,
                    address: {
                        street: null,
                        city: null,
                        country: null
                    },
                    url: null
                }
            }
        },
        //钩子函数 链式编程 ES6新特性
        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/"
    }
  ]
}
相关标签: 前端开发 vue