下载:
npm install vue-router
路由的传参:
1.查询字符串:
/user/regist?uname=jack&upwd=123
接收:
{{$route.query}}
2.rest风格传参
/user/login/rose/456
接收:
{{$route.params}}
<div id='app'>
<router-link to='/home'>首页</router-link>
<router-link to='/user'>用户页</router-link>
<router-view></router-view>
</div>
<script src="https://cdn.bootcss.com/vue/2.5.16/vue.min.js"></script>
<script src="https://cdn.bootcss.com/vue-router/3.0.1/vue-router.min.js"></script>
<script>
//2
var Home={
template:`
<div>我是首页</div>
`
}
var User={
template:`
<div>
我是用户页
<p>
<router-link to='/user/regist?uname=jack&upwd=123'>注册</router-link>
</p>
<p>
<router-link to='/user/login/rose/456'>登录</router-link>
</p>
<router-view></router-view>
</div>
`
}
var Regist={
template:`
<div>
<h1>这是注册页面</h1>
<a href="">{{$route.query}}</a>
<a href="">uname:{{$route.query.uname}}</a>
<a href="">upwd:{{$route.query.upwd}}</a>
</div>
`
}
var Login={
template:`
<div>
<h1>这是登录页面</h1>
<a>{{$route.params}}</a>
<a>{{$route.params.uname}}</a>
<a>{{$route.params.upwd}}</a>
</div>
`
}
//3.
const routes=[
{path:'/',component:Home},
{path:'/home',component:Home},
{
path:'/user',
component:User,
children:[
{path:'regist',component:Regist},
{path:'login/:uname/:upwd',component:Login}
]
}
]
//4.创建实例
const router=new VueRouter({
routes:routes
})
new Vue({
el:"#app",
router:router
})
</script>