vue通过axios,实现从后端服务获取json格式数据。再进行前端展示。
<!DOCTYPE html>
<html lang="en" xmlns:v-bind="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<title>Title</title>
<!-- v-clock 解决闪烁问题-->
<style>
[v-clock]{
display: none;
}
</style>
</head>
<body>
<div id="vue" v-clock>
<div>{{info.name}}</div>
<div>{{info.address.street}}</div>
<a v-bind:href="info.url">链接点击</a>
</div>
<!-- 导入vue js-->
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.21/dist/vue.min.js"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script type="text/javascript">
var vm=new Vue({
el: "#vue",
mounted(){
axios.get('../data.json').then(response=>(this.info=response.data))//这里赋值
},
data(){
return{//data函数返回 接收的数据
info: {//结构要与json一致,可以少几个
name: null,
url: null,
address: {
street: null,
city: null,
country: null
}
}
}
}
});
</script>
</body>
</html>