vue router生命周期
router中的參數
router異步解析執行,此時router在 resolve 完之前一直處於 等待中。
三個參數:
to: Route: 即將要進入的目標 路由對象
from: Route: 當前導航正要離開的路由
next: Function: 一定要調用該方法來 resolve 這個鈎子
組件中路由的生命周期中的不同方法:
beforeRouteUpdate(to, from, next) {
this.type = to.params.type;
this.onPullingDown();
}
組件中路由的生命周期中的不同方法:
beforeRouteEnter (to, from, next) {
// 在渲染該組件的對應路由被 confirm 前調用
// 不!能!獲取組件實例 `this`
// 因為當守衛執行前,組件實例還沒被創建
},
// 對於一個帶有動態參數的路徑 /foo/:id,在 /foo/1 和 /foo/2 之間跳轉的時候,由於會渲染同樣的 Foo 組件,因此組件實例會被復用。而這個鈎子就會在這個情況下被調用。
beforeRouteUpdate (to, from, next) {
// 在當前路由改變,但是該組件被復用時調用
// 可以訪問組件實例 `this`
},
// 這個離開守衛通常用來禁止用戶在還未保存修改前突然離開。該導航可以通過 next(false) 來取消。
beforeRouteLeave (to, from, next) {
// 導航離開該組件的對應路由時調用
// 可以訪問組件實例 `this`
}
Vue中生命周期總結
根組件實例:8個 (beforeCreate、created、beforeMount、mounted、beforeUpdate、updated、beforeDestroy、destroyed)
組件實例:8個 (beforeCreate、created、beforeMount、mounted、beforeUpdate、updated、beforeDestroy、destroyed)
全局路由鈎子:2個 (beforeEach、afterEach)
組件路由鈎子:3個 (beforeRouteEnter、beforeRouteUpdate、beforeRouteLeave)
指令的周期: 5個 (bind、inserted、update、componentUpdated、unbind)
beforeRouteEnter的next所對應的周期
nextTick所對應的周期