第一种:axios
1.获取模拟假数据(mock-json)----get获取
<script src="https://cdn.bootcss.com/vue/2.6.10/vue.js"></script>
<script src="https://cdn.bootcss.com/axios/0.19.0-beta.1/axios.min.js"></script>
{
"id": 1,
"name": "小明"
}
<body>
<div id="app">
<button @click = "getData"> 获取mock数据 </button>
</div>
</body>
<script>
new Vue({
el: '#app',
methods: {
getData () {
axios({
url: './data/data.json',
method: 'get'
})
.then( res => console.log( res ))
.catch( error => console.log( error ))
}
}
})
</script>
2.获取后端api(php文件)----get获取
<body>
<div id="app">
<button @click = "getPhp"> 获取后端api </button>
</div>
</body