<div class="box">
<router-link to='/home'>首頁(yè)</router-link>
<router-link to='/detail'>詳情頁(yè)</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>這是首頁(yè)</h1>
`
}
var Detail = {
template:`
<div>
<h1>這是詳情頁(yè)</h1>
<table>
<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.uname}}</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 thiss = this//這里用了vue的生命周期為了下面可以應(yīng)用this所以需要聲明一個(gè)代替this
axios({
method:'get',//請(qǐng)求方式
url:'fruit.json',//axios數(shù)據(jù)的位置
}).then(function(tru){//請(qǐng)求成功
thiss.list = tru.data
console.log(thiss.list);
}).catch(function(flash){//請(qǐng)求失敗
})
}
}
const routes = [
{path:"/",component:Home},
{path:"/home",component:Home},
{path:"/detail",component:Detail}
]
const router = new VueRouter({
routes:routes
})
new Vue({
el:'.box',
router:router
})
</script>
fruit.json
[
{
"num":1,
"uname":"apple",
"price":3,
"count":4,
"sub":12
},
{
"num":2,
"uname":"banana",
"price":4,
"count":5,
"sub":20
},
{
"num":3,
"uname":"pear",
"price":6,
"count":5,
"sub":30
}
]