两类:
1.查询字符串:
例: /user/regist?uname=jack&upwd=123
接收:
{{$route.query}}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script src="js/vue.js"></script>
<script src="js/vue-router.js"></script>
</head>
<body>
<div id="app">
<router-link to='/home'>首页</router-link>
<router-link to='/user'>用户页</router-link>
<router-view></router-view>
</div>
<script>
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'>登录</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>
</div>
`
}
const routes=[
{path:'/',component:Home},
{path:'/home',component:Home},
{
path:'/user',
component:User,
children:[
{path:'regist',component:Regist},
{path:'login/:uname/:upwd',component:Login}
]
}
]
const router=new VueRouter({
routes:routes
})
new Vue({
el:'#app',
router:router
})
</script>
</body>
</html>
2.rest风格传参
例: /user/login/rose/456
接收:
{{$route.params}}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script src="js/vue.js"></script>
<script src="js/vue-router.js"></script>
</head>
<body>
<div id="app">
<router-link to='/home'>首页</router-link>
<router-link to='/user'>用户页</router-link>
<router-view></router-view>
</div>
<script>
var Home={
template:`
<div>我是首页</div>
`
}
var User={
template:`
<div>
我是用户页
<p>
<router-link to='/user/regist'>注册</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>
</div>
`
}
var Login={
template:`
<div>
<h1>这是登录页</h1>
<a>{{$route.params}}</a>
<a>{{$route.params.uname}}</a>
<a>{{$route.params.upwd}}</a>
</div>
`
}
const routes=[
{path:'/',component:Home},
{path:'/home',component:Home},
{
path:'/user',
component:User,
children:[
{path:'regist',component:Regist},
{path:'login/:uname/:upwd',component:Login}
]
}
]
const router=new VueRouter({
routes:routes
})
new Vue({
el:'#app',
router:router
})
</script>
</body>
</html>
网友评论