1提完、用git安裝$ npm install axios
2、$ npm install http-server -g下載
1
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
/*
.router-link-active{
color:red;
}
*/
.active{
color:red;
}
</style>
</head>
<body>
<a></a>
<div id='app'>
<!--1.-->
<router-link to='/home'>首頁(yè)</router-link>
<router-link to='/user'>用戶頁(yè)</router-link>
<!-- 盛放鏈接對(duì)應(yīng)的內(nèi)容-->
<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>
//2.創(chuàng)建組件
var Home={
template:`
<h1>這是首頁(yè)</h1>
`
}
var User={
template:`
<div>
<h1>這是用戶頁(yè)</h1>
<table border=1 cellspacing=0>
<thead>
<tr>
<th>編號(hào)</th>
<th>品名</th>
<th>單價(jià)</th>
<th>數(shù)量</th>
<th>小計(jì)</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",//get post
url:'fruit.json'
}).then(function(resp){//請(qǐng)求成功
console.log(resp.data)
self.list=resp.data;
}).catch(function(err){//請(qǐng)求失敗
console.log(err)
})
}
}
//3.配置路由
const routes=[
{path:'/',component:Home},
{path:'/home',component:Home},
{path:'/user',component:User}
]
//4.創(chuàng)建路由實(shí)例
const router=new VueRouter({
routes:routes,
linkActiveClass:'active'
})
//5.路由實(shí)例掛載到vue實(shí)例上
new Vue({
el:'#app',
router:router
})
</script>
</body>
</html>
2丘侠、新建一個(gè)json后綴名的文件徒欣,然后在里面寫數(shù)據(jù)
[
{
"num":1,
"pname":"apple",
"price":3,
"count":2,
"sub":6
},
{
"num":2,
"pname":"pear",
"price":4,
"count":3,
"sub":12
},
{
"num":3,
"pname":"banana",
"price":5,
"count":4,
"sub":20
}
]