最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

vue生命周期的探索

 更新時間:2019年04月03日 09:24:18   作者:曾廣營  
這篇文章主要介紹了vue生命周期的探索,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

那么進入某個路由對應(yīng)的組件的時候,我們會觸發(fā)哪些類型的周期呢?

  1. 根實例的加載相關(guān)的生命周期(beforeCreate、created、beforeMount、mounted)
  2. 組件實例的加載相關(guān)的生命周期(beforeCreate、created、beforeMount、mounted)
  3. 全局路由勾子(router.beforeEach)
  4. 組件路由勾子(beforeRouteEnter)
  5. 組件路由勾子的next里的回調(diào)(beforeRouteEnter)
  6. 指令的周期(bind,inserted)
  7. nextTick方法的回調(diào)

接下來,讓我們用vue-cli簡單改造后的項目,做一個測試,看看各個聲明周期的觸發(fā)順序是怎樣的

main.js:

router.beforeEach((to, from, next) => {
 console.log('路由全局勾子:beforeEach')
 next()
})
 
router.afterEach((to, from) => {
 console.log('路由全局勾子:afterEach')
})
 
new Vue({
 beforeCreate () {
  console.log('根組件:beforeCreate')
 },
 created () {
  console.log('根組件:created')
 },
 beforeMount () {
  console.log('根組件:beforeMount')
 },
 mounted () {
  console.log('根組件:mounted')
 }
 el: '#app',
 router,
 template: '<App/>',
 components: { App }
})

test.vue

<template>
 <h1 v-ooo @click = "$router.push('/')">test</h1>
</template>
<script>
export default {
 beforeRouteEnter (to, from, next) {
  console.log('組件路由勾子:beforeRouteEnter')
  next(vm => {
   console.log('組件路由勾子beforeRouteEnter的next')
  })
 },
 beforeCreate () {
  console.log('組件:beforeCreate')
 },
 created () {
  this.$nextTick(() => {
   console.log('nextTick')
  })
  console.log('組件:created')
 },
 beforeMount () {
  console.log('組件:beforeMount')
 },
 mounted () {
  console.log('組件:mounted')
 },
 directives: {
  ooo: {
   bind (el, binding, vnode) {
    console.log('指令binding')
   },
   inserted (el, binding, vnode) {
    console.log('指令inserted')
   }
  }
 }
}
</script>

接下來,直接進入test.vue對應(yīng)的路由。在控制臺,我們看到如下的輸出

我們看到執(zhí)行的順序為

  1. 路由勾子 (beforeEach、beforeRouteEnter、afterEach)
  2. 根組件 (beforeCreate、created、beforeMount)
  3. 組件 (beforeCreate、created、beforeMount)
  4. 指令 (bind、inserted)
  5. 組件 mounted
  6. 根組件 mounted
  7. beforeRouteEnter的next的回調(diào)
  8. nextTick

結(jié)論

路由勾子執(zhí)行周期非常早,甚至在根實例的渲染之前

具體的順序 router.beforeEach > beforeRouteEnter > router.afterEach

tip:在進行路由攔截的時候要避免使用實例內(nèi)部的方法或?qū)傩浴?br /> 在開發(fā)項目時候,我們腦門一拍把,具體攔截的程序,寫在了根實例的方法上了,到beforeEach去調(diào)用。
結(jié)果導(dǎo)致整個攔截的周期,推遲到實例渲染的之后。
因此對于一些路由組件的beforeRouteEnter里的請求并無法攔截,頁面看上去好像已經(jīng)攔截下來了。
實際上請求依然發(fā)了出去,beforeRouteEnter內(nèi)的函數(shù)依然執(zhí)行了。

指令的綁定在組件mounted之前,組件的beforeMount之后
不得不提的, beforeRouteEnter的next勾子

beforeRouteEnter的執(zhí)行順序是如此靠前,而其中next的回調(diào)勾子的函數(shù),執(zhí)行則非常靠后,在mounted之后??!
我們通常是在beforeRouteEnter中加載一些首屏用數(shù)據(jù),待數(shù)據(jù)收到后,再調(diào)用next勾子,通過回調(diào)的參數(shù)vm將數(shù)據(jù)綁定到實例上。
因此,請注意next的勾子是非??亢蟮摹?/p>

nextTick
越早注冊的nextTick觸發(fā)越早

以上所述是小編給大家介紹的vue生命周期的探索詳解整合,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

相關(guān)文章

最新評論

昌邑市| 白山市| 沙雅县| 海盐县| 温州市| 株洲县| 那坡县| 蓬溪县| 龙州县| 宁明县| 兴国县| 张家港市| 金寨县| 兖州市| 济宁市| 三河市| 象山县| 前郭尔| 易门县| 平顶山市| 宜黄县| 孝昌县| 新余市| 阿拉善盟| 阿克| 紫阳县| 桂林市| 桐乡市| 黑水县| 博乐市| 高州市| 东阿县| 建水县| 汝阳县| 湟源县| 南平市| 沅陵县| 疏勒县| 综艺| 临清市| 株洲县|