路由vue-route的使用示例教程
一、項(xiàng)目初始化
二、路由配置規(guī)則
path:配置路由訪問的路徑
name:給路由起名字(命名路由)
component:訪問路由時(shí),渲染的組件
{ path: '/', name: 'index', component: () => import('../views/IndexView.vue') },
App.vue
vue-route標(biāo)簽作用:路由匹配到的組件將渲染到這里
<template> <router-view/> </template>
router-link標(biāo)簽作用:路由導(dǎo)航(路由跳轉(zhuǎn)的鏈接)
三、聲明式導(dǎo)航和編程式導(dǎo)航
聲明式導(dǎo)航
<router-link to="/login"></router-link> <router-link :to="{path:'/login'}"></router-link>
編程式導(dǎo)航
推薦使用路由的名字進(jìn)行跳轉(zhuǎn),不推薦直接寫路徑
<button @click="$router.push('/login')">登錄按鈕</button> <button @click="$router.push({path:'/login'})">登錄按鈕</button> <button @click="$router.push({name:'login'})">登錄按鈕</button>
$router
:路由對象
在app.use(router)在注冊路由時(shí),會給app設(shè)置全局屬性$router
<button @click="loginBtn">登錄按鈕</button> <script> export default{ methods:{ loginBtn(){ this.$router.push('/login') } } } </script>
通過調(diào)用app.use(router),我們可以在任意組件中以this.$router
的形式訪問它,并且以this.$router
的形式訪問當(dāng)前路由
四、路由重定向
當(dāng)訪問http://localhost:8080/#/project這個(gè)路由
會跳轉(zhuǎn)到http://localhost:8080/#/login這個(gè)路由
{ path: '/project', name:'project', // 路由重定向配置 redirect:{ name:'login', } },
五、嵌套路由
index.js:路由配置
{ path: '/home', name: 'home', component: () => import('../views/HomeView.vue'), // 配置home下面的且套路由 children:[ { path:'/home/project', name:'project', component:()=>import('../views/ProjectView.vue') }, { path:'/home/interface', name:'interface', component:()=>import('../views/InterfaceView.vue') }, { path:'/home/report', name:'report', component:()=>import('../views/ReportView.vue') }] },
HomeView.vue組件
<template> <el-menu :default-active="activeIndex" class="el-menu-demo" mode="horizontal" @select="handleSelect"> <el-menu-item index="1" @click="$router.push({name:'project'})">項(xiàng)目信息</el-menu-item> <el-menu-item index="2" @click="$router.push({name:'interface'})">接口信息</el-menu-item> <el-menu-item index="3" @click="$router.push({name:'report'})">測試報(bào)告</el-menu-item> </el-menu> <!-- home中嵌套路由的渲染位置(路由出口) --> <router-view/> </template> <script> </script> <style> </style>
特別注意
把不變的內(nèi)容寫到父路由中,并且父路由中預(yù)留路由展示位。將變化的內(nèi)容寫到子路由中
總結(jié)
六、路由參數(shù)動(dòng)態(tài)匹配
{ path:'/user/:id', name:'user', component: () => import('../views/UserView.vue') },
訪問路由:http://localhost:8080/#/user/666
UserView.vue組件
獲取路由的路徑參數(shù)
<template> <h1>User頁面</h1> <!-- 獲取路由的路徑參數(shù) --> <h3>路由中匹配的id:{{$route.params.id}}</h3> </template> <script> </script> <style> </style>
獲取路由的查詢參數(shù)
http://localhost:8080/#/user/666?name=kobe
<template> <h1>User頁面</h1> <!-- 獲取路由的查詢參數(shù) --> <h4>查詢參數(shù)name:{{$route.query.name}}</h4> </template> <script> </script> <style> </style>
特別注意
$router
和$route
的區(qū)別:$router
:路由管理器對象,一般用于路由跳轉(zhuǎn)$route
:表示當(dāng)前訪問的路由,用來獲取當(dāng)前路由參數(shù)的一些信息
七、導(dǎo)航跳轉(zhuǎn)時(shí)傳遞路由參數(shù)
<router-link :to="{name:'user',params:{id:888},query:{name:111}}">user頁面</router-link> <button @click="$router.push({name:'user',params:{id:666},query:{name:222}})">user按鈕</button>
八、路由導(dǎo)航守衛(wèi)
設(shè)置路由導(dǎo)航守衛(wèi)(控制前端的路由訪問權(quán)限)
router.beforeEach(async (to, from) => { /* 1、判斷用戶是否登錄 1.1從本地獲取用戶身份信息(存儲在cookie或者localstroge中的token,session) window.cookieStore.get('token') window.localStorage.getItem('token') window.sessionStore.getItem('token') 1.2驗(yàn)證token是否有效 */ // const isAuthenticated=true // if ( // // 檢查用戶是否已登錄 // !isAuthenticated && // // ?? 避免無限重定向 // to.name !== 'Login' // ) { // // 將用戶重定向到登錄頁面 // return { name: 'Login' } // } // }) })
到此這篇關(guān)于路由vue-route的使用的文章就介紹到這了,更多相關(guān)路由vue-route使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- vue3使用vue-router及路由權(quán)限攔截方式
- Vue3使用vue-router如何實(shí)現(xiàn)路由跳轉(zhuǎn)與參數(shù)獲取
- vue-router重定向和路由別名的使用講解
- Vue-router的使用和出現(xiàn)空白頁,路由對象屬性詳解
- 使用vue-router為每個(gè)路由配置各自的title
- 使用vue-router beforEach實(shí)現(xiàn)判斷用戶登錄跳轉(zhuǎn)路由篩選功能
- 使用vue-route 的 beforeEach 實(shí)現(xiàn)導(dǎo)航守衛(wèi)(路由跳轉(zhuǎn)前驗(yàn)證登錄)功能
- vue-router:嵌套路由的使用方法
- VueJs路由跳轉(zhuǎn)——vue-router的使用詳解
相關(guān)文章
vue完美實(shí)現(xiàn)el-table列寬自適應(yīng)
這篇文章主要介紹了vue完美實(shí)現(xiàn)el-table列寬自適應(yīng),對vue感興趣的同學(xué),可以參考下2021-05-05Vue頁面跳轉(zhuǎn)動(dòng)畫效果的實(shí)現(xiàn)方法
百度了好久都沒辦法實(shí)現(xiàn)vue中一個(gè)頁面跳到另一個(gè)頁面,甚至到google上搜索也是沒辦法的,最終還是找了高人親自指導(dǎo),所以下面這篇文章主要給大家介紹了關(guān)于Vue頁面跳轉(zhuǎn)動(dòng)畫效果的實(shí)現(xiàn)方法,需要的朋友可以參考下2018-09-09vue3利用v-model實(shí)現(xiàn)父子組件之間數(shù)據(jù)同步的代碼詳解
在Vue 3中,v-model這一指令也得到了升級,使得父子組件之間的數(shù)據(jù)同步變得更加容易和靈活,本文將探討一下Vue 3中如何利用v-model來實(shí)現(xiàn)父子組件之間的數(shù)據(jù)同步,需要的朋友可以參考下2024-03-03淺談vue中document.getElementById()拿到的是原值的問題
這篇文章主要介紹了淺談vue中document.getElementById()拿到的是原值的問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07安裝vue無法運(yùn)行、此系統(tǒng)無法運(yùn)行腳本問題及解決
這篇文章主要介紹了安裝vue無法運(yùn)行、此系統(tǒng)無法運(yùn)行腳本問題及解決,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03