Axios异步通信

  1. 先创建data.json文件
{
  "name": "wen",
  "url": "https://www.cnblogs.com/hellowen/",
  "address": {
    "country": "中国",
    "city": "你猜"
  },
  "links": [
    {
      "name": "Google",
      "url": "http://www.google.com"
    },
    {
      "name": "Baidu",
      "url": "http://www.baidu.com"
    }
  ]
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

    <div id="app">
        <div>{{info.name}}</div>
        <div>{{info.address.country}}</div>
        <a v-bind:href="info.url">点我</a>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.js"></script>
    <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script>
    var vm = new Vue({
        el:"#app",
        data(){
            //请求的返回参数格式,必须和json字符串一样
            return{
                info:{
                    name:null,
                    url:null,
                    address:{
                        country:null,
                        city:null
                    }
                }
            }
        },
        mounted(){ //钩子函数
            axios.get('data.json').then(response => this.info = response.data)
        }
    })
</script>
</body>
</html>

最终效果

注意:

猜你喜欢

转载自www.cnblogs.com/hellowen/p/12912353.html