Vue
、Re:Vue 重頭說起
、Alex 宅幹嘛
Vue 幫你自動把 $route 傳給 component,讓你方便取道 query 與 params
Using $route
in your component creates a tight coupling with the route which limits the flexibility of the component as it can only be used on certain URLs.
如果你在component內用到 $route
參數 代表 這個component只能for這個 $route
的 url 做使用,有此參數就不用把 router的狀態根vuex做同步
$router 要把component與router做結耦
$router 是Component向上要資料
props 是 傳資料進 component
結果 query 拿得到,params 拿不到
用 props 好處,只告我自己的資料與別人給的props,這樣可以降低結耦合(Ex: 每一個模組都讀 router 與 vuex 會提高結耦合)
此法較佳,index模組不限定只能在router或是哪個網址才能使用,只要有傳 id 死 test 都可以使用
## HTML5 History Mode (33:45)
History mode > Hash mode
41:10 預設是 hash mode,要用 history mode要對 server做處理
如果要使用 history mode,要請後端把所有頁面的route交由前端處理,並都指向同一個檔案 index.html page,
處理server的方式如下:
For Node.js/Express, consider using connect-history-api-fallback middleware.
Add this to your firebase.json
:
因為任何頁面都會導來我們這邊,所以 server 不會處理,都是 200 OK,所以前端要處理 router 404
更多 Vue SSR 知識:https://ssr.vuejs.org/
watch query 或 hook 都可以實現功能
match 判斷是哪些與路徑有符合搭配到,目前只偵測到一個(1:05:00 不是很懂?)
1:09:27 Global Before Guards 文件參數講解
1:16:30 請問登入該坐在哪一個HOOK? A. beforeEach B.beforeEach C. beforeEnter Ans: 大型網站的話:beforeEach 搭配 判斷式,單頁網站就 beforeEnter
確保 next() 只被呼叫一次,要這樣寫:
The beforeRouteEnter guard does NOT have access to this, because the guard is called before the navigation is confirmed, thus the new entering component has not even been created yet.
beforeRouteUpdate 就可以拿到 this
離開網頁時,被阻擋與詢問
E可以在每一個路由加一些參數與資料,例如:在beforeEach做驗證,但頁面或路由器很多,所以我們就可以針對每一個路由去判斷是否要驗證,此時可以用 meta,Ex:requiresAuth
動態路由 & 另一種權限管控?!
Transitions 動態效果有時候有,有時候沒有的主要原因是 Vue 沒有辦法依照現有狀態判斷你是否有切換,是以解決辦法是要加Key,所以可以用 $route.fullPath
當 key ,Ex: fullPath: /bbb?test=456
,假設在同一個company或路由當資料改變時,但不想被做動畫可以用 $route.name
或 $route.path
Ex: name: index
or path: '/bbb'
把 transition 用動態綁定,然後去 watch 這個 route,也可以用 hook 去控制 route 動畫效果 Ex: beforeRouteEnter
討論:在進到頁面前Fetch資料,還是在進到Component在Fetch資料?
Fetching After Navigation: perform the navigation first, and fetch data in the incoming component's lifecycle hook. Display a loading state while data is being fetched.先導頁,接下來再Component內透過生命週期拿資料,適合頁面專用資料
Fetching Before Navigation: Fetch data before navigation in the route enter guard, and perform the navigation after data has been fetched.切換路由之前,hook先做資料處理,適合做驗證打API拿資料,之後再換頁
在元件的 created 做 fetchData()
beforeRouteUpdate、beforeRouteEnter
QA: 1:41:28 在 route 去 access store的資料
The user will stay on the previous view while the resource is being fetched for the incoming view. It is therefore recommended to display a progress bar or some kind of indicator while the data is being fetched. If the data fetch fails, it's also necessary to display some kind of global warning message.
這段Code沒事初始化專案時可以先寫,因為當single page切頁時,當使用者捲到某個位置,但是你切了一頁後,捲軸不回彈到最上面,會停在同一個位置,所以加了 scrollBehavior ,捲軸就會置頂
搭配 webpack 做延遲載入
webpackChunkName 不要亂命名,webpackChunkName名字要一樣 1:47:48
1:50:05 分配路由的經驗
如果用 router-link
component 不會出錯,使用 router.push
or router.replace
就會出錯(Uncaught (in promise) Error
)
來看看 router-link 與 button 的差別
router-link 是可以指定 tag 的,所以不需要很複雜的判斷式
tag 預設是 有 href 屬性支援,若不是 href 會自動被抽調,但是原本class樣式會保留
可以這樣設計動態切頁按鈕:router 是 /bbb 按鈕可以按,反之,按鈕無法按 (1:58:06)
如果用 <button> 做此功能判斷式會比較麻煩
很少人用此功能,此功能是你要如何去接這個錯誤,或對錯誤行為做處理(Ex:把按鈕關掉/做錯誤擷取),但push新的頁面時,可以把錯誤catch起來
Navigation Failures are Error instances with a few extra properties. To check if an error comes from the Router, use the isNavigationFailure function:(2:13:18)
有一個網友提出他們家的做法:把原本的push做調整,加上了 catch 共用化,任何錯誤都進入 catch
錯誤可以透過 to
與form
參數觀察
TIP: 如果你省略 isNavigationFailure 第一個的參數 failure,他只會幫你確定這是不是Navigation的錯誤,不會幫你分類類別(NavigationFailureType)
Ex: 剛剛例子在 /bbb 下,點擊 button 進入 /bbb 就會噴 duplicated type 的路由 error,如下圖