<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.text{
color: brown;
}
</style>
</head>
<body>
<div id="itany">
<router-link to="/home">首页</router-link>
<router-link to="/user">用户页</router-link>
<router-view></router-view>
</div>
<script src='dist/vue.js'></script>
<script src="dist/vue-router.js"></script>
<script>
//2.创造组件
var Home={
template:`
<h1>这是首页</h1>
`
}
var User={
template:`
<div>
<h1>这是用户页</h1>
<ul>
<li>
//查询字符串
<router-link to="/User/regist?uname=jack&pack=123">登录</router-link>
</li>
<li>
<router-link to="/User/login/rose/456">注册</router-link>
</li>
</ul>
<router-view></router-view>
</div>
`
}
var Regist={
template:`
<div>
<h3>这是注册页</h3>
//接收:
<a href="">{{$route.query.uname}}</a>
<a href="">{{$route.query.pack}}</a>
</div>
`
}
var Login={
template:`
<div>
<h3>这是登录页</h3>
<a href="">{{$route.params.uname}}</a>
<a href="">{{$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}
]
}
]
//4.创建路由实例
const router=new VueRouter({
routes:routes,
linkActiveClass:'text'
})
new Vue({
el:'#itany',
router:router
})
</script>
</body>
</html>
路由传参有两个方式:params 与 query
两种方式的区别
1.query要用path来引入,params要用name来引入,接收参数都是类似的,分别是this.route.params.name。
2.query更加类似于我们ajax中get传参,params则类似于post,说的再简单一点,前者在浏览器地址栏中显示参数,后者则不显示