偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

Vue Router 在實際項目中用到的 10 條高級技巧

開發(fā) 前端
Vue Router 是 Vue.js 官方的路由管理器。它和 Vue.js 的核心深度集成,讓構建單頁面應用變得易如反掌。

前言

Vue Router 是 Vue.js 官方的路由管理器。

它和 Vue.js 的核心深度集成,讓構建單頁面應用變得易如反掌。

[[397463]]

包含的功能有:

  • 嵌套的路由/視圖表
  • 模塊化的、基于組件的路由配置
  • 路由參數(shù)、查詢、通配符
  • 基于 Vue.js 過渡系統(tǒng)的視圖過渡效果
  • 細粒度的導航控制
  • 帶有自動激活的 CSS class 的鏈接
  • HTML5 歷史模式或 hash 模式,在 IE9 中自動降級
  • 自定義的滾動條行為

本文是作者是實際項目中遇到的一些總結,主要包括:

  1. 響應路由參數(shù)變化
  2. 路由匹配
  3. 高級匹配模式
  4. 匹配優(yōu)先級
  5. push和replace的第二個第三個參數(shù)
  6. 路由視圖
  7. 重定向
  8. 使用props解耦$route
  9. 導航守衛(wèi)
  10. 守衛(wèi)的next方法

希望本文對你有所幫助。

正文

1. 響應路由參數(shù)變化

針對復用組件(只是路由參數(shù)發(fā)生改變),生命周期函數(shù)鉤子不會被調用,如何能刷新組件了?

watch監(jiān)聽 

  1. watch: { 
  2.   '$route' (tofrom) { 
  3.   // 對路由變化作出響應... 
  4.   } 

beforeRouteUpdate 

  1. beforeRouteUpdate (tofromnext) { 
  2. // react to route changes... 
  3. / / don't forget to call next() 

2. 路由匹配 

  1. // 會匹配所有路徑 
  2. path: '*' 
  3. // 會匹配以 `/user-` 開頭的任意路徑 
  4. path: '/user-*' 

注意:當使用通配符路由時,請確保路由的順序是正確的,也就是說含有通配符的路由應該放在最后。路由 { path: '*' } 通常用于客戶端 404 錯誤。

如果你使用了History 模式,請確保正確配置你的服務器。

當使用一個通配符時,$route.params 內(nèi)會自動添加一個名為 pathMatch 參數(shù)。

它包含了 URL 通過通配符被匹配的部分: 

  1. // 給出一個路由 { path: '/user-*' } 
  2. this.$router.push('/user-admin'
  3. this.$route.params.pathMatch // 'admin' 
  4. // 給出一個路由 { path: '*' } 
  5. this.$router.push('/non-existing'
  6. this.$route.params.pathMatch // '/non-existing' 

3. 高級匹配模式 

  1. // 命名參數(shù)必須有"單個字符"[A-Za-z09]組成 
  2.   
  3. // ?可選參數(shù) 
  4. { path: '/optional-params/:foo?' } 
  5. // 路由跳轉是可以設置或者不設置foo參數(shù),可選 
  6. <router-link to="/optional-params">/optional-params</router-link> 
  7. <router-link to="/optional-params/foo">/optional-params/foo</router-link> 
  8.   
  9. // 零個或多個參數(shù) 
  10. { path: '/optional-params/*' } 
  11. <router-link to="/number">沒有參數(shù)</router-link> 
  12. <router-link to="/number/foo000">一個參數(shù)</router-link> 
  13. <router-link to="/number/foo111/fff222">多個參數(shù)</router-link> 
  14.   
  15.   
  16. // 一個或多個參數(shù) 
  17. { path: '/optional-params/:foo+' } 
  18. <router-link to="/number/foo">一個參數(shù)</router-link> 
  19. <router-link to="/number/foo/foo111/fff222">多個參數(shù)</router-link> 
  20.   
  21. // 自定義匹配參數(shù) 
  22. // 可以為所有參數(shù)提供一個自定義的regexp,它將覆蓋默認值([^\/]+) 
  23. { path: '/optional-params/:id(\\d+)' } 
  24. { path: '/optional-params/(foo/)?bar' } 

4. 匹配優(yōu)先級

有時候一個路徑可能匹配多個路由。

此時,匹配的優(yōu)先級就是按照路由的定義順序:先定義,優(yōu)先級最高。

5. push和replace的第二個第三個參數(shù)

在 2.2.0+版本,可選的在 router.push 或 router.replace 中提供 onComplete 和 onAbort 回調作為第二個和第三個參數(shù)。

這些回調將會在導航成功完成 (在所有的異步鉤子被解析之后) 或終止 (導航到相同的路由、或在當前導航完成之前導航到另一個不同的路由) 的時候進行相應的調用。在 3.1.0+,可以省略第二個和第三個參數(shù),此時如果支持 Promise,router.push 或 router.replace 將返回一個 Promise。

接下來看幾個例子來看看第二個第三個參數(shù)的調用時機:

1. 組件1跳轉組件2 

  1. // 組件1 
  2. this.$router.push({ name'number' }, () => { 
  3.   console.log('組件1:onComplete回調'); 
  4. }, () => { 
  5.   console.log('組件1:onAbort回調'); 
  6. }); 
  7.  
  8. // 組件2 
  9. beforeRouteEnter(tofromnext) { 
  10.   console.log('組件2:beforeRouteEnter'); 
  11.   next(); 
  12. }, 
  13. beforeCreate() { 
  14.   console.log('組件2:beforeCreate'); 
  15. }, 
  16. created() { 
  17.   console.log('組件2:created'); 

 

組件之間跳轉觸發(fā)onComplete回調。

2. 組件2跳轉組件2(不帶參數(shù)) 

  1. this.$router.push({ name'number'}, () => { 
  2.   console.log('組件2:onComplete回調'); 
  3. }, () => { 
  4.   console.log('組件2,自我跳轉:onAbort回調'); 
  5. }); 

 

組件自我跳轉當不帶參數(shù)時觸發(fā)onAbort回調。但是當自我跳轉帶參數(shù)時可能情況就有點不一樣。

3. 組件2跳轉組件2(帶參數(shù)) 

  1. this.$router.push({ name'number', params: { foo: this.number}}, () => { 
  2.     console.log('組件2:onComplete回調'); 
  3. }, () => { 
  4.     console.log('組件2,自我跳轉:onAbort回調'); 
  5. }); 

 

組件自我?guī)?shù)跳轉,onComplete回調、onAbort回調回調都不會觸發(fā)。

6. 路由視圖

有時候想同時 (同級) 展示多個視圖,而不是嵌套展示,例如創(chuàng)建一個布局,有 sidebar (側導航) 和 main (主內(nèi)容) 兩個視圖,這個時候命名視圖就派上用場了。

你可以在界面中擁有多個單獨命名的視圖,而不是只有一個單獨的出口。

如果 router-view 沒有設置名字,那么默認為 default。

  1. <router-view class="view one"></router-view
  2. <router-view class="view two" name="a"></router-view
  3. <router-view class="view three" name="b"></router-view

一個視圖使用一個組件渲染,因此對于同個路由,多個視圖就需要多個組件。

確保正確使用 components 配置 (帶上 s): 

  1. const router = new VueRouter({ 
  2. routes: [ 
  3.   { 
  4.     path: '/'
  5.     components: { 
  6.         default: Foo, 
  7.         a: Bar, 
  8.         b: Baz 
  9.     } 
  10.     } 
  11.   ] 
  12. }); 

7. 重定向 

  1. { path: '/a', redirect: '/b' } 
  2. { path: '/a', redirect: { name'foo' }} 
  3. { path: '/a', redirect: to => { 
  4.   // 方法接收 目標路由 作為參數(shù) 
  5.   // return 重定向的 字符串路徑/路徑對象 
  6. }} 

注意:導航守衛(wèi)并沒有應用在跳轉路由上,而僅僅應用在其目標上。

在上面這個例子中,為 /a 路由添加一個 beforeEach 或 beforeLeave 守衛(wèi)并不會有任何效果。

8. 使用props解耦$route

在組件中使用 $route 會使之與其對應路由形成高度耦合,從而使組件只能在某些特定的 URL 上使用,限制了其靈活性。 

  1. // router文件 
  2. // 對于包含命名視圖的路由,你必須分別為每個命名視圖添加 `props` 選項: 
  3.   path: '/number/:name'
  4.   props: true
  5.   // 對象模式 props: { newsletterPopup: false } 
  6.   // 函數(shù)模式 props: (route) => ({ query: route.parmas.name }) 
  7.   name'number'
  8.   component: () => import( /* webpackChunkName: "number" */ './views/Number.vue'
  9.  
  10. // 組件獲取 
  11. export default
  12.   props: ['name'

9. 導航守衛(wèi)

1. 三種全局守衛(wèi)

  • router.beforeEach 全局前置守衛(wèi) 進入路由之前。
  • router.beforeResolve 全局解析守衛(wèi)2.5.0新增。在beforeRouteEnter調用之后調用。
  • router.afterEach 全局后置鉤子 進入路由之后。 
  1. // 入口文件 
  2. import router from './router' 
  3.   
  4. // 全局前置守衛(wèi) 
  5. router.beforeEach((tofromnext) => { 
  6. console.log('beforeEach 全局前置守衛(wèi)'); 
  7. next(); 
  8. }); 
  9. // 全局解析守衛(wèi) 
  10. router.beforeResolve((tofromnext) => { 
  11. console.log('beforeResolve 全局解析守衛(wèi)'); 
  12. next(); 
  13. }); 
  14. // 全局后置守衛(wèi) 
  15. router.afterEach((tofrom) => { 
  16. console.log('afterEach 全局后置守衛(wèi)'); 
  17. }); 

2. 路由獨享守衛(wèi)

beforeEnter全局前置守衛(wèi)進入路由之前。 

  1.   path: '/number/:name'
  2.   props: true
  3.   name'number'
  4.   // 路由獨享守衛(wèi) 
  5.   beforeEnter: (tofromnext) => { 
  6.       console.log('beforeEnter 路由獨享守衛(wèi)'); 
  7.       next(); 
  8.   }, 
  9.   component: () => import( /* webpackChunkName: "number" */ './views/Number.vue'

 

3. 組件內(nèi)守衛(wèi)

  • beforeRouteEnter
  • beforeRouteUpdate(2.2新增)
  • beforeRouteLeave 
  1. beforeRouteEnter(tofromnext) { 
  2.   // 在渲染該組件的對應路由被 confirm 前調用 
  3.   // 不!能!獲取組件實例 `this` 
  4.   // 因為當守衛(wèi)執(zhí)行前,組件實例還沒被創(chuàng)建 
  5.   console.log('beforeRouteEnter 組件內(nèi)進入守衛(wèi)'); 
  6.   next(); 
  7. }, 
  8. beforeRouteUpdate(tofromnext) { 
  9.   // 在當前路由改變,但是該組件被復用時調用 
  10.   // 舉例來說,對于一個帶有動態(tài)參數(shù)的路徑 /foo/:id,在 /foo/1 和 /foo/2 之間跳轉的時候, 
  11.   // 由于會渲染同樣的 Foo 組件,因此組件實例會被復用。而這個鉤子就會在這個情況下被調用。 
  12.   // 可以訪問組件實例 `this` 
  13.   console.log('beforeRouteUpdate 組件內(nèi)更新守衛(wèi)'); 
  14.   next(); 
  15. }, 
  16. beforeRouteLeave(tofromnext) { 
  17.   // 導航離開該組件的對應路由時調用 
  18.   // 可以訪問組件實例 `this` 
  19.   console.log('beforeRouteLeave 組件內(nèi)離開守衛(wèi)'); 
  20.   next(); 
  • 組件1跳轉到組件2,然后組件2跳轉組件2本身 

  • 組件1跳轉到組件2,然后組件2跳轉組件1

10. 守衛(wèi)的 next 方法

  • next: 調用該方法 resolve 鉤子。
  • next(): 進行管道中的下一個鉤子。如果全部鉤子執(zhí)行完了,則導航的狀態(tài)就是 confirmed (確認的)。
  • next(false): 中斷當前的導航。如果瀏覽器的 URL 改變了 (可能是用戶手動或者瀏覽器后退按鈕),那么 URL 地址會重置到 from 路由對應的地址。
  • next('/') 或者 next({ path: '/' }): 跳轉到一個不同的地址。當前的導航被中斷,然后進行一個新的導航。你可以向 next 傳遞任意位置對象,且允許設置諸如 replace: true、name: 'home' 之類的選項以及任何用在 router-link 的 to prop 或 router.push 中的選項。
  • next(error): (2.4.0+) 如果傳入 next 的參數(shù)是一個 Error 實例,則導航會被終止且該錯誤會被傳遞給 router.onError() 注冊過的回調。

最后

最終還是希望大家多看看文檔,理解了再去使用到項目中,不至于使用之后出現(xiàn) bug,謝謝。

 

責任編輯:未麗燕 來源: Segmentfault.com
相關推薦

2024-01-22 09:43:50

數(shù)據(jù)庫方案

2019-03-25 10:30:19

開發(fā)技能代碼

2014-07-09 14:33:12

PythonPython運維

2023-10-12 14:22:45

2022-04-22 10:51:45

TSaxios前端

2024-01-22 13:15:00

2020-06-30 08:28:29

Vue開發(fā)前端

2024-09-09 05:30:00

數(shù)據(jù)庫Spring

2021-08-28 10:06:29

VueJavascript應用

2021-05-19 09:29:52

VueAxios異步請求

2020-04-17 20:58:34

MySQLSQL優(yōu)化

2017-11-27 11:25:36

MySQL優(yōu)化數(shù)據(jù)

2024-04-03 09:03:05

項目分支管理

2020-12-16 10:20:15

Arrow Python 時間序列

2010-01-21 13:04:53

3Com千兆以太網(wǎng)交換

2018-11-07 09:39:03

Runtime開發(fā)項目

2010-03-18 10:45:33

Python Djan

2018-06-29 11:50:55

UbuntuLinux技巧

2010-05-11 14:23:05

Unix命令

2023-09-14 08:46:50

ReactVue
點贊
收藏

51CTO技術棧公眾號