6.0Vue-router
6.1相关理解
6.1.1vue-router的理解
vue的一个插件库,专门用来实现spa应用
6.1.2对spa应用的理解
1.单个web应用
2.整个应用只有一个完整的页面
3.点击页面中的导航链接不会刷新页面,只会做页面的局部更新
4.数据需要经过ajax请求
6.1.3路由的理解
1.什么是路由
1.一个路由就是一组映射关系
2.key为路径,value可能是function或者component
2.路由的分类
1.后端路由
1.理解:value是function,用于处理客户端提交的请求
2.工作过程:服务器接收到一个请求时,根据请求路径找到匹配的函数来处理请求,返回响应数据
2.前端路由
1.理解:value是component,用于展示页面内容
2.工作过程:当浏览器的路径改变时,对应的组件就会展示
6.2基本路由
6.2.1效果
6.2.2基本使用
1.安装vue-router
2.应用插件
3.编写配置项
//该文件专门用于创建整个应用的路由器
import VueRouter from 'vue-router'
//引入组件
import About from '../components/About'
import Home from '../components/Home'
//创建并暴露一个路由器
export default new VueRouter({
routes:[
{
path:'/about',
component:About
},
{
path:'/home',
component:Home
},
]
})
4.实现切换
<router-link class="list-group-item" active-class="active" to="/home">Home</router-link>
6.2.3注意点
1.路由组件通常存放在pages文件里面,一般组件通常存放在components文件夹
2.通过切换,隐藏了路由组件,默认是被销毁的,需要的时候再去挂载
3.每个组件都有自己的$route属性,里面存储这自己的路由信息
4.整个应用只有一个router,可以通过组件的$router属性获取
6.2.4嵌套(多级)路由
routes:[
{
path:'/about',
component:About
},
{
path:'/home',
component:Home,
children:[//通过children配置子级路由
{
path:'news',//不能写/news
component:News
},
{
path:'message',
component:Message
}
]
}
]
跳转
<router-link to="/home/news">news</router-link>
6.2.5路由传参
传递参数
<router-link :to="{
path:'/home/message/detail',
query:{
id:m.id,
title:m.title
}
}">
{{m.title}}
</router-link>
接收参数
<li>消息的编号:{{$route.query.id}}</li>
<li>消息的标题:{{$route.query.title}}</li>
6.2.6命名路由
作用:可以简化路由的跳转
6.2.7路由的params参数
<router-link :to="{
name:'xiangqing',
params:{
id:m.id,
title:m.title
}
}">
6.2.8路由的props配置
作用:让路由组件更方便的收到参数
name:'xiangqing',
path:'detail/:id/:title',
component:Detail,
//props的第一种写法,值为对象,该对象中的所有key-value都会以props的形式传给detail组件
// props:{a:1,b:'hello'}
//第二种写法,值为布尔值,若为真,就会把路由组件收到的所有parmas参数,以props的形式传给detail组件
// props:true,
//第三种写法,值为函数,
props($route){
return {id:$route.query.id,title:$route.query.title}
}
6.2.9路由的replace属性
1.作用:控制路由跳转时操作浏览器历史记录的模式
2.浏览器的历史记录有两种写入方法,分别是push和replace,push是追加历史记录,replace是替换当前几率,路由跳转的时候默认为push
3.如何开启replace模式
<router-link replace ......>news</router-link>
6.2.10编程式路由导航
1.作用:不借助实现路由跳转,让路由跳转更加灵活
2.具体编码
pushShow(m){
this.$router.push({
name:'xiangqing',
query:{
id:m.id,
title:m.title
}
})
},
replaceShow(m){
this.$router.replace({
name:'xiangqing',
query:{
id:m.id,
title:m.title
}
})
}
this.$router.forward()//前进
this.$router.back()//后退
this.$router.go(-2)//跳转
6.2.11缓存路由组件
1.作用:让不展示的路由组件保持挂载,不被销毁
<!-- 一定要写组件名,这是缓存一个 -->
<!-- <keep-alive include="News"> -->
<!-- 这是缓存多个 -->
<keep-alive :include="['News','Message']">
<router-view></router-view>
</keep-alive>
6.2.12两个新的生命周期钩子
1.作用:路由组件所独有的两个钩子,用于捕获路由组件的激活状态
2.具体名字
1.activated:路由组件被激活时触发
2.deactivated:路由组件失活时触发
6.2.13路由守卫
1.作用:保护路由的安全(权限)
2.分类:全局守卫,独享守卫,组件内守卫
全局守卫:
//全局前置路由守卫--初始化的时候被调用,每次路由切换之前被调用
router.beforeEach((to,from,next)=>{
if(to.meta.isAuth) {//判断是否需要鉴权
if(localStorage.getItem('school') === 'atguigu'){
next()
}else{
alert('学校名不对')
}
}else{
next()
}
})
//全局后置路由守卫
router.afterEach((to,from)=>{
document.title = to.meta.title || '硅谷系统'
})
独享路由守卫
children:[
{
name:'xinwen',
path:'news',
component:News,
meta:{isAuth:true,title:'新闻'},
beforeEnter:(to,from,next) => {
if(to.meta.isAuth) {//判断是否需要鉴权
if(localStorage.getItem('school') === 'atguigu'){
next()
}else{
alert('学校名不对')
}
}else{
next()
}
}
},
组件内路由守卫
//通过路由规则,进入该组件时被调用
beforeRouteEnter(to,from,next){
if(to.meta.isAuth) {//判断是否需要鉴权
if(localStorage.getItem('school') === 'atguigu'){
next()
}else{
alert('学校名不对')
}
}else{
next()
}
},
//通过路由规则,离开该组件时被调用
beforeRouteLeave(to,from,next){
next()
}
6.2.14history和hash模式
1.对于一个url来说,什么是hash值,----#及其后面的内容就是hash值
2.hash值不会包含在http请求中,即,hash值不会带给服务器
3.hash模式:
1.地址中永远带着#不好看
2.若以后将地址通过第三方手机app分享,若app校验严格,则地址会被标记为不合法
3.兼容性好
4.history模式
1.地址干净,美观
2.兼容性和hash模式相比略差
3.应用部署上线后需要后端人员支持,解决刷新页面服务端404问题
7.0vueui组件库
7.1移动端ui组件库
1.vant
2.cubeui
3.mint ui
7.2pc端ui组件库
1.element ui
2.lview ui