axios
vue的 ajax(前端页面和后台数据做交互)。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script src='js/vue.js'></script>
<script src='js/vue-router.js'></script>
<script src='js/axios.js'></script>
<style>
*{
text-align: center;
}
.active{
color:red;
}
</style>
</head>
<body>
<div id="app">
<router-link to='/home'>首页</router-link>
<router-link to='/user'>用户页</router-link>
<router-view></router-view>
</div>
<script>
var Home={
template:`
<h1>俺是首页</h1>
`
}
var User={
template:`
<div>
<h1>俺是用户页</h1>
<table border=1 cellspacing=0 text-center>
<thead style="background:red;">
<tr>
<th>编号</th>
<th>品名</th>
<th>单价</th>
<th>数量</th>
<th>小计</th>
</tr>
</thead>
<tbody style="background:red;">
<tr v-for="value in list">
<td>{{value.num}}</td>
<td>{{value.pname}}</td>
<td>{{value.price}}</td>
<td>{{value.count}}</td>
<td>{{value.sub}}</td>
</tr>
</tbody>
</table>
</div>
`,
data:function(){
return{
list:null
}
},
mounted:function(){
var self=this;
axios({
method:'get',
url:'2.json'
}).then(function(suc){
console.log(suc.data);
self.list=suc.data
}).catch(function(wro){
console.log(wro)
})
}
}
const xxx=[
{path:'/home',component:Home},
{path:'/user',component:User}
]
const www=new VueRouter({
routes:xxx,
linkActiveClass:'active'
})
new Vue({
el:'#app',
router:www
})
</script>
</body>
</html>
[
{
"num":1,
"pname":"苹果",
"price":3,
"count":2,
"sub":6
},
{
"num":2,
"pname":"梨子",
"price":4,
"count":4,
"sub":16
},
{
"num":3,
"pname":"香蕉",
"price":6,
"count":3,
"sub":18
}
]