编程式导航、缓存路由组件、路由守卫、Vue UI组件库【VUE】

6.5 编程式路由导航

  1. 作用:不借助<router-link> 实现路由跳转,让路由跳转更加灵活

  2. 具体编码:

    //$router的两个API
    this.$router.push({
    	name:'xiangqing',
    		params:{
    			id:xxx,
    			title:xxx
    		}
    })
    
    this.$router.replace({
    	name:'xiangqing',
    		params:{
    			id:xxx,
    			title:xxx
    		}
    })
    this.$router.forward() //前进
    this.$router.back() //后退
    this.$router.go() //可前进也可后退
    

6.6 缓存路由组件

  1. 作用:让不展示的路由组件保持挂载,不被销毁
  2. 具体编码:
<keep-alive include="News">
	<router-view></router-view>
</keep-alive>

6.7 两个新的声明周期钩子

  1. 作用:路由组件所独有的两个钩子,用于捕获路由组件的激活状态。
  2. 具体名字:
    (1)activated路由组件被激活时触发
    (2)deactivated路由组件失活时触发

6.8 路由守卫

  1. 作用:对路由进行权限控制
  2. 分类:全局守卫、独享守卫、组件内守卫
  3. 全局守卫:
//全局前置守卫:初始化时执行、每次路由切换前执行
   router.beforeEach((to,from,next)=>{
   	console.log('beforeEach',to,from)
   	if(to.meta.isAuth){ //判断当前路由是否需要进行权限控制
   		if(localStorage.getItem('school') === 'atguigu'){ //权限控制的具体规则
   			next() //放行
   		}else{
   			alert('暂无权限查看')
   			// next({name:'guanyu'})
   		}
   	}else{
   		next() //放行
   	}
   })
   
   //全局后置守卫:初始化时执行、每次路由切换后执行
   router.afterEach((to,from)=>{
   	console.log('afterEach',to,from)
   	if(to.meta.title){ 
   		document.title = to.meta.title //修改网页的title
   	}else{
   		document.title = 'vue_test'
   	}
   })

router/index.js

//该文件专门用于创建整个应用的路由器
import VueRouter from 'vue-router'
//引入组件
import About from '../pages/About'
import Home from '../pages/Home'
import News from '../pages/News'
import Message from '../pages/Message'
import Detail from '../pages/Detail'

//创建一个路由器并暴露一个路由器
const router =  new VueRouter({
    routes: [
        {
            name: 'guanyu',
            path: '/about',
            component: About,
            meta: {title: '关于'}
        },
        {
            name: 'zhuye',
            path: '/home',
            component: Home,
            meta: {title: '主页'},

            children: [
                {
                    path: 'news',
                    component: News,
                    meta: {isAuth: true, title: '新闻'}

                },
                {
                    name: 'xiaoxi',
                    path: 'message',
                    component: Message,
                    meta: {isAuth: true, title: '消息'},
                    children: [
                        {
                            name:'xiangqing',
                            path: 'detail',
                            component: Detail,
                            meta: {isAuth: true, title: '详情'},

                            // props 的第一种写法,值为对象,该对象中的所有 key-value都会以props的形式传给Detail组件
                            // props: {a:1, b:'hello'}

                            // props 的第一种写法,值为布尔值,若布尔值为真,就会把该路由组件收到的所有params参数,以props形式传给Detail组件
                            // props: true,

                            // props 的第一种写法,值为函数
                            props($route){
                                return {id: $route.query.id, title: $route.query.title}
                            }
                        }
                    ]
                }
            ]
        },
        
    ]
})
// 全局前置路由守卫 ———— 初始化的时候被调用、每次路由切换之前被调用
router.beforeEach((to, from, next) => {
    console.log('前置路由守卫', to, from);
    if(to.meta.isAuth){ //判断是否需要鉴权
        if(localStorage.getItem('school') === 'xianwenli'){
            next()
        }else{
            alert('学校名不对,无权限查看!')
        }
    }else{
        next()
    }
})

// 全局后置路由守卫 ———— 初始化的时候被调用、每次路由切换之后被调用
router.afterEach((to, from) => {
    console.log('后置路由守卫', to, from);
    document.title = to.meta.title || '系统'
})
export default router
  1. 独享守卫
   beforeEnter(to,from,next){
   	console.log('beforeEnter',to,from)
   	if(to.meta.isAuth){ //判断当前路由是否需要进行权限控制
   		if(localStorage.getItem('school') === 'atguigu'){
   			next()
   		}else{
   			alert('暂无权限查看')
   			// next({name:'guanyu'})
   		}
   	}else{
   		next()
   	}
   }
  1. 组件内路由守卫
   //进入守卫:通过路由规则,进入该组件时被调用
   beforeRouteEnter (to, from, next) {
   },
   //离开守卫:通过路由规则,离开该组件时被调用
   beforeRouteLeave (to, from, next) {
   }

6.9 路由器的两种工作模式

  1. 对于一个url来说,什么是hash值?——#及其后面的内容就是hash值。
  2. hash值不会包含在HTTP请求中,即:hash值不会带给服务器。
  3. hash模式:
    (1)地址中永远带着#号,不美观。
    (2)若以后地址通过第三方收集app分享,若app校验严格,则地址会被标记为不合法。
    (3)兼容性好
  4. history模式:
    (1)地址干净,美观。
    (2)兼容性和hash模式相比略差。
    (3)应用部署上线时需要后端人员的支持,解决刷新页面服务端404的问题。

第七章:Vue UI组件库

7.1 移动端常用 UI 组件库

  1. Vant https://youzan.github.io/vant
  2. Cube UI https://didi.github.io/cube-ui
  3. Mint UI http://mint-ui.github.io

7.2 PC端常用 UI 组件库

  1. Element UI https://element.eleme.cn
  2. IView UI https://www.iviewui.com
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
THE END
分享
二维码

)">
< <上一篇
下一篇>>