vue路由插件之vue-route
vue路由插件,vuer Router,使vue官方的路由管理其,和vue高度耦合
1.vue-Router的使用
import Vue from 'vue' import Router from 'vue-router' //引入路由組件 Vue.use(Router) new Router({ mode: 'history', //路由的兩種模式 hash 和history 默認使history模式 routes: [ { path: '/', name: 'home', component: () => import(xxx.vue) }, { path: '/about', name: 'about', component: () => import() } ] })
2.路由的跳轉(zhuǎn)
this.$router.push('/path')
this.$router.push({name:'routername'})
路由的get方式傳值
this.$router.push({name:'routername',query:{id:xxx}})
路由的post方式傳值
this.$router.push({name:'routername',params:{id:xxx}})
3.路由的后退
this.$router.go(-1)
this.$router.back()
4.路由的前進
this.$router.forward()
5.替換當前路由,在路由歷史中不會再出現(xiàn)該路由
this.$router.replace(location)
6.當前路由的對象屬性(一定要記得是小寫的$route,并且沒有r)
this.$route.path 當前路由路徑 path
this.$route.name 當前路由名稱
this.$route.params.id post方式傳參時,獲取id的值
this.$route.query.id get方式傳參時獲取id的值
this.$route.hash 當前路由的hash值,帶#
7.linkActiveClass
當前激活的路由的class類名,默認是"router-link-active"
8.scrollBehavior
切換路由時頁面滾動到具體位子
9.router-link 中的tag標簽,生成具體的標簽的html 元素
10.router-view 路由組件具體渲染的地方
11.全部的路由鉤子函數(shù)(導航首位)
11.1router.beforeEach 全局前置首位
11.2router.beforeResolve 全局解析守衛(wèi)
11.3router.afterEach 全局后置守衛(wèi)
11.4beforeEnter 路由獨享守衛(wèi)
組件內(nèi)守衛(wèi)
11.5beforerouteEnter 進入
11.6beforerouteUpdate 更新
11.7beforerouteLeave 離開
/* 全局前置守衛(wèi) */ router.beforeEach(function (to, from, next) { // to 將要進路的路由 route // from 離開的路由 route // next 進入下一個路由,不調(diào)用則不會進入下一個路由 console.log('全局前置守衛(wèi)') next() }) /* 全局解析守衛(wèi) */ router.beforeResolve((to, from, next) => { // to 將要進路的路由 route // from 離開的路由 route console.log('全局解析守衛(wèi)') next() }) /* 全局后置守衛(wèi) */ router.afterEach((to, from) => { // to 將要進路的路由 route // from 離開的路由 route console.log('全局后置守衛(wèi)') }) /* 組件獨享守衛(wèi) */ beforeEnter(to, from, next) { console.log('組件內(nèi)獨享守衛(wèi)') next() }
beforeRouteEnter(to, from, next) { console.log('組件內(nèi)守衛(wèi)進入') next() }, beforeRouteUpdate(to, from, next) { console.log('組件內(nèi)守衛(wèi)更新') next() }, beforeRouteLeave(to, from, next) { console.log('組件內(nèi)守衛(wèi)離開前') next() }
執(zhí)行順序,
1.前組件內(nèi)守衛(wèi)離開
2.全局前置守衛(wèi)
3.路由獨享守衛(wèi)
4.組件內(nèi)守衛(wèi)進入
5.全局解析守衛(wèi)
6.全局后置守衛(wèi)
或者時刷新組件時(/about 跳轉(zhuǎn)到/about?id=1111)
1.全局前置守衛(wèi)
2.組件內(nèi)守衛(wèi)更新
3.全局解析守衛(wèi)
4.全局后置守衛(wèi)
總結(jié)
以上所述是小編給大家介紹的vue路由vue-route的實例代碼,非常不錯,具有一定的參考借鑒價值,需要的朋友參考下吧!
相關(guān)文章
Vue使用sign-canvas實現(xiàn)在線手寫簽名的實例
sign-canvas?一個基于?canvas?開發(fā),封裝于?Vue?組件的通用手寫簽名板(電子簽名板),支持?pc?端和移動端,本文給大家分享Vue使用sign-canvas實現(xiàn)在線手寫簽名,感興趣的朋友一起看看吧2022-05-05Vue elementUI 自定義表單模板組件功能實現(xiàn)
在項目開發(fā)中,我們會遇到這種需求,在管理后臺添加自定義表單,在指定的頁面使用定義好的表單,這篇文章主要介紹了Vue elementUI 自定義表單模板組件,需要的朋友可以參考下2022-12-12Vue3異步數(shù)據(jù)加載組件suspense的使用方法
前端開發(fā)中異步請求是非常常見的事情,比如遠程讀取圖片,調(diào)用后端接口等等,這篇文章主要給大家介紹了關(guān)于Vue3異步數(shù)據(jù)加載組件suspense的使用方法,suspense中文含義是懸念的意思,需要的朋友可以參考下2021-08-08vue實現(xiàn)動態(tài)控制el-table表格列的展示與隱藏
這篇文章主要介紹了vue實現(xiàn)動態(tài)控制el-table表格列的展示與隱藏,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-04-04