首先,需要理解一下vue-router:
vue-router是vue的一个插件,专门用来实现SPA应用。SPA也就是单页Web应用,特点是:整个应
用只有一个完整的页面,点击页面中的导航链接不会刷新页面,只会做页面的局部更新,数据需要
通过ajax请求获取。
什么是路由:
1、一个路由就是一组映射关系(key-value)
2、key为路径,value可能是function或component
路由的分类:
1、后端路由
(1)理解:value是function,用于处理客户端提交的请求。
(2)工作过程:服务器接收到一个请求时,根据请求路径找到匹配的函数来处理请求,返回响应数据。
2、前端路由
(1)理解:value是component,用于展示页面内容。
(2)工作过程:当浏览器的路径改变时,对应的组件就会显示。
首先需要在main.js中配置路由:
import Vue from 'vue'
import App from './App.vue'
import VueRouter from 'vue-router'
import router from './router'
Vue.config.productionTip = false
Vue.use(VueRouter)
new Vue({
el:'#app',
render: h => h(App),
router:router
}).$mount('#app')
实现切换(active-class可配置高亮样式):
(
1、作用:控制路由跳转时操作浏览器历史记录的模式;
2、浏览器的历史记录有两种写入模式:分别为push和replace,push是追加历史记录,replace是替换当前记录,路由跳转时候默认为push;
3、直接在
指定显示位置:
几个注意点:
1、路由组件通常存放在pages文件夹,一般组件通常存放在components文件夹;
2、通过切换,“隐藏”了的路由组件,默认是被销毁掉的,需要的时候再去挂载;
3、每个组件都有自己的$route属性,里面存储着自己的路由信息;
4、整个应用只有一个router,可以通过组件的$router属性获取到。
1、配置路由规则,使用children配置项:
routes:[
{
path:'/about',
component:About
},
{
path:'/home',
component:Home,
children:[
{
path:'news',
component:News
},
{
path:'message',
component:Message
}
]
}
]
2、跳转的时候要写完整路径:
1、传递参数
{{m.title}}
2、接收参数
1、配置路由,声明接收params参数
routes:[
{
path:'/about',
component:About
},
{
path:'/home',
component:Home,
children:[
{
path:'news',
component:News
},
{
path:'message',
component:Message,
children:[
{
name:'xiangqing',//必须通过name的方式找到details
path:'details',//使用占位符声明接收params参数
component:Details
}
]
}
]
}
]
2、传递参数
{{m.title}}
3、接收参数
1、作用:不借助
2、编码:
methods:{
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
}
})
}
}
1、作用:让不展示的路由组件保持挂载,不被销毁。
2、具体代码:
1、作用:路由组件所独有的两个钩子,用于捕获路由组件的激活状态。
2、具体名字:
actived:路由组件被激活时触发;
deactived:路由组件失活时触发。
1、作用:对路由进行权限控制;
2、分类:全局守卫、独享守卫、组件内守卫;
3、全局守卫:
全局前置守卫,初始化时执行,每次路由切换前执行
router.beforeEach((to,from,next)=>{
if(to.meta.isAuth){//判断是否需要鉴权
if(localStorage.getItem('school')==='hkd'){
next()
}
}
else{
next()
}
})
全局后置路由守卫,初始化时执行,每次路由切换之后被调用
router.afterEach(()=>{
if(to.meta.isAuth){//判断是否需要鉴权
document.title=to.meta.title
}
else{
document.title='vue_test'
}
})
4、独享路由守卫:某一个路由所独享的;
beforeEnter:(to,from,next)=>{
if(to.meta.isAuth){//判断是否需要鉴权
if(localStorage.getItem('school')==='hkd'){
next()
}
}
else{
next()
}
}
5、组件内路由守卫
//通过路由规则进入该组件时调用
beforeRouteEnter(to,from,next){
console.log('beforeRouteEnter');
if(to.meta.isAuth){//判断是否需要鉴权
if(localStorage.getItem('school')==='hkd'){
next()
}
}
else{
next()
}
},
//通过路由规则离开该组件时调用
beforeRouteLeave(to,from,next){
next()
}