一.VueRouter简介
Vue.js 路由允许我们通过不同的 URL 访问不同的内容.
通过 Vue.js 可以实现多视图的单页Web应用(single page web application,SPA)
Vue.js 路由需要载入 vue-router 库
二、安装VueRouter
1.直接下载 / CDN
https://unpkg.com/vue-router/dist/vue-router.js
2.使用npm下载
npm install vue-router
三、路由案例
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
/*
.router-link-active{
color:red;
}
*/
.active{
color:red;
}
</style>
</head>
<body>
<a href=""></a>
<div id='app'>
<!--1.-->
<!-- 使用 router-link 组件来导航. -->
<!-- 通过传入 `to` 属性指定链接. -->
<!-- <router-link> 默认会被渲染成一个 `<a>` 标签 -->
<router-link to='/index'>首页</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>
//2. 创建组件
var Index={
template:`
<h1>这是首页</h1>
`
}
var Detail={
template:`
<h1>我是详情页</h1>
`
}
//3.配置路由
const routes=[
{path:'/',component:Index},
{path:'/index',component:Index},
{path:'/detail',component:Detail}
]
//4.创建一个路由实例
const router=new VueRouter({
routes:routes,
linkActiveClass:"active"
})
//5.把路由实例挂在到vue实例上
new Vue({
el:'#app',
router:router
})
</script>
</body>
</html>
四、路由的嵌套
<!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='js/vue.js'></script>
<script src='js/vue-router.js'></script>
<script>
//2.创建组件
var Home={
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:Home},
{path:'/home',component:Home},
{
path:'/user',
component:User,
children:[
{path:'regist',component:Regist},
{path:'login',component:Login}
]
}
]
//4.创建路由实例
const router=new VueRouter({
routes:routes
})
//5.
new Vue({
el:'#app',
router:router//注册路由
})
</script>
</body>
</html>
五、路由的传参
<!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='js/vue.js'></script>
<script src='js/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&&upwd=123'>注册</router-link>
</li>
<li>
<!--rest风格传参-->
<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}}</a>
<ul>
<li>{{$route.query.uname}}</li>
<li>{{$route.query.upwd}}</li>
</ul>
</div>
`
}
var Login={
template:`
<div>
<h3>这是登录页</h3>
<a href="">{{$route.params}}</a>
<ul>
<li>{{$route.params.userName}}</li>
<li>{{$route.params.password}}</li>
</ul>
</div>
`
}
//3.配置路由
const routes=[
{path:'/',component:Home},
{path:'/home',component:Home},
{
path:'/user',
component:User,
children:[
{path:'regist',component:Regist},
{path:'login/:userName/:password',component:Login}
]
}
]
//4.创建路由实例
const router=new VueRouter({
routes:routes
})
//5.
new Vue({
el:'#app',
router:router//注册路由
})
</script>
</body>
</html>
网友评论