1、axios知识点
(1)axios是Vue中的ajax,axios分为1.0版本(Vue-resource)和2.0版本(axios库)。
(2)ajax是前端网页和后台数据做交互。
2、axios的下载:
npm install axios
例:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<div class="app">
<router-link to="/home">首页</router-link>
<router-link to="/user">用户页</router-link>
<router-view></router-view>
</div>
<script src="js/vue.js"></script>
<script src="js/vue-router.js"></script>
<script src="js/axios.js"></script>
<script>
var home={
template:`
<h1>这是首页</h1>
`
}
var user={
template:`
<h1>这是用户页</h1>
`
}
const routes=[
{path:"/",component:home},
{path:"/home",component:home},
{path:"/user",component:user}
]
const router=new VueRouter({
routes:routes
})
new Vue({
el:".app",
router:router
})
</script>
</body>
</html>
3、http-server的安装
npm install http-server -g
4、开启一个服务
http-server
http-server的练习
HTML部分:
<div class="app">
<router-link to="/home">首页</router-link>
<router-link to="/user">用户页</router-link>
<router-view></router-view>
</div>
java script的练习
<script src="js/vue.js"></script>
<script src="js/vue-router.js"></script>
<script src="js/axios.js"></script>
<script>
var home={
template:`
<h1>这是首页</h1>
`
}
var user={
template:`
<div>
<h1>这是用户页</h1>
<table border=1>
<thead>
<tr>
<th>编号</th>
<th>品名</th>
<th>单价</th>
<th>数量</th>
<th>小计</th>
</tr>
</thead>
<tbody>
<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:"index.json"
}).then(function(resp){
console.log(resp.data)
self.list=resp.data;
}).catch(function(err){
console.log(err)
})
}
}
const routes=[
{path:"/",component:home},
{path:"/home",component:home},
{path:"/user",component:user}
]
const router=new VueRouter({
routes:routes
})
new Vue({
el:".app",
router:router
})
</script>
json文件部分
[
{
"num":1,
"pname":"苹果",
"price":2,
"count":3,
"sub":6
},
{
"num":2,
"pname":"香蕉",
"price":3,
"count":4,
"sub":12
},
{
"num":3,
"pname":"桃子",
"price":4,
"count":5,
"sub":20
},
{
"num":4,
"pname":"柚子",
"price":5,
"count":6,
"sub":30
}
]
运行结果为:
image.png
网友评论