1.路由:
具体步骤:
1.>router-link to=' 链接 ';
<router-view></router-view> 对应链接下显示的内容;
2.>创建组件:var index={ };
3.>配置路由:
const routes=[
{path:'/',component:Index},
{path:'/index',component:Index},
{path:'/user',component:User}
]
4.>创建路由实例:
const router=new VueRouter({routes:routes})
5.>把路由实例挂载到vue实例上;
new Vue({
el:'#app',
router:router //注册路由
})
2.路由实例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
/*
.router-link-active{
color:hotpink;
}
*/
.active{
color:blue;
}
</style>
</head>
<body>
<div id='app'>
<!--1.-->
<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.js"></script>
<script src="https://cdn.bootcss.com/vue-router/3.0.1/vue-router.js"></script>
<script>
//2.创建组件
var Home={
template:`
<h1>这是首页</h1>
`
}
var User={
template:`
<h1>这是用户页</h1>
`
}
//3.配置路由
const routes=[
{path:'/home',component:Home},
{path:'/user',component:User}
]
//4.创建路由实例
const router=new VueRouter({
routes:routes,
linkActiveClass:'active'
})
//5.路由实例挂载到vue实例上
new Vue({
el:'#app',
router:router
})
</script>
</body>
</html>
2.路由的传参:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<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>
</body>
</html>
3.路由的嵌套:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<div id='app'>
<!--1.-->
<router-link to='/index'>首页</router-link>
<router-link to='/user'>用户页</router-link>
<router-view></router-view>
</div>
<script src="https://cdn.bootcss.com/vue/2.5.16/vue.js"></script>
<script src="https://cdn.bootcss.com/vue-router/3.0.1/vue-router.js"></script>
<script>
//2.创建组件
var Index={
template:`
<h1>这是首页</h1>
`
}
var User={
template:`
<div>
<h1>这是用户页</h1>
<ul>
<li>
<router-link to='/user/regist'>注册</router-link>
</li>
<li>
<router-link to='/user/login'>登录</router-link>
</li>
</ul>
<router-view></router-view>
</div>
`
}
var Regist={
template:`
<h3>这是注册页</h3>
`
}
var Login={
template:`
<h3>这是登录页</h3>
`
}
//3.配置路由
const routes=[
{path:'/',component:Index},
{path:'/index',component:Index},
{
path:'/user',
component:User,
children:[
{path:'regist',component:Regist},
{path:'login',component:Login}
]
}
]
//4.创建路由实例
const router=new VueRouter({
routes:routes
})
//5.把路由实例挂载到vue实例上
new Vue({
el:'#app',
router:router//注册路由
})
</script>
</body>
</html>