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

Vue Axios异步请求

程序员文章站 2022-07-02 12:38:57
...

数据

{
  "name": "shouhe",
  "age": 18,
  "isBoy": true,
  "house": {
    "location": "zhejiang",
    "port": 8080,
    "num": [0,1,2,3]
  }
}

完整的写法:

<!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>
</head>
<body>
<div id="app">
    <h3>{{info}}</h3>
    <h3>{{info.name}}</h3>
    <h3>{{info.age}}</h3>
</div>
<script src="https://lib.baomitu.com/vue/2.6.11/vue.min.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.19.2/axios.js"></script>
<script>
    var vm = new Vue({
        el: "#app",
        data() {
            return {
                info: {
                    name: null,
                    isBoy: null,
                    age: null,
                    house: {
                        location: null,
                        port: null,
                        num: null
                    }

                }
            }
        },
        mounted() {
            axios.get('data.json').then(response => (this.info = response.data));
        }
    });
</script>
</body>
</html>

简略的写法:

<!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>
</head>
<body>
<div id="app">
    <h3>{{info}}</h3>
    <h3>{{info.name}}</h3>
    <h3>{{info.age}}</h3>
</div>
<script src="https://lib.baomitu.com/vue/2.6.11/vue.min.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.19.2/axios.js"></script>
<script>
    var vm = new Vue({
        el: "#app",
        data() {
            return {
                info: {}
                }
            }
        },
        mounted() {
            axios.get('data.json').then(response => (this.info = response.data));
        }
    });
</script>
</body>
</html>