<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
*{
padding: 0px;
margin: 0px;
}
.router-link-active{
color: #2aabd2;
}
li{
list-style: none;
float: left;
padding-left: 30px;
}
</style>
</head>
<body>
<div id='app'>
<!--//1.-->
<router-link to='/home'>首页</router-link>
<router-link to='/detail'>详情页</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>
//2.创建组件
var Home={
template:`
<h1>这是首页</h1>
`
}
var Detail={
template:`
<div>
<h1>这是用户页</h1>
<ul>
<li>
<router-link to='/detail/zhuce'>注册</router-link>
</li>
<li>
<router-link to='/detail/denglu'>登录</router-link>
</li>
</ul>
<table border="1" cellspacing="0" width="800px">
<thead>
<tr>
<th>编号</th>
<th>名称</th>
<th>单价</th>
<th>数量</th>
<th>小计</th>
</tr>
</thead>
<tbody>
<tr v-for="w in funes">
<td>{{w.num}}</td>
<td>{{w.pname}}</td>
<td>{{w.price}}</td>
<td>{{w.count}}</td>
<td>{{w.sub}}</td>
</tr>
</tbody>
</table>
<router-view></router-view>
</div>
`,
data:function(){
return{
funes:null
}
},
mounted:function(){
var self=this;
axios({
method:'get',
url:'fruit.json'
}).then(function(resp){
self.funes=resp.data
}).catch(function(err){
})
}
}
var Zhuce={
template:`
<h4>这是注册页面</h4>
`
}
var Denglu={
template:`
<h4>这是登录页面</h4>
`
}
//3.配置路由
const routes=[
{path:'/',component:Home},
{path:'/home',component:Home},
{
path:'/detail',
component:Detail,
children:[
{path:'zhuce',component:Zhuce},
{path:'denglu',component:Denglu},
]
}
]
//4.创建一个路由实例
const router=new VueRouter({
routes:routes
})
//把路由挂在到vue实例上
new Vue({
el:'#app',
router:router
})
</script>
</body>
</html>
网友评论