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

Vue生命周期詳解

 更新時(shí)間:2022年06月23日 16:54:45   作者:小旭2021  
本文詳細(xì)講解了Vue的生命周期,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

這篇博客將會(huì)從下面四個(gè)常見的應(yīng)用詮釋組件的生命周期,以及各個(gè)生命周期應(yīng)該干什么事

  • 單組件的生命周期
  • 父子組件的生命周期
  • 兄弟組件的生命周期
  • 宏mixin的生命周期

生命周期:Vue 實(shí)例從開始創(chuàng)建、初始化數(shù)據(jù)、編譯模板、掛載Dom→渲染、更新→渲染、卸載等一系列過(guò)程,我們稱這是 Vue 的生命周期,各個(gè)階段有相對(duì)應(yīng)的事件鉤子

1. 生命周期鉤子函數(shù)

下面這張圖是vue生命周期各個(gè)階段的執(zhí)行情況:

注意:

  • created階段的ajax請(qǐng)求與mounted請(qǐng)求的區(qū)別:前者頁(yè)面視圖未出現(xiàn),如果請(qǐng)求信息過(guò)多,頁(yè)面會(huì)長(zhǎng)時(shí)間處于白屏狀態(tài)

  • mounted 不會(huì)承諾所有的子組件也都一起被掛載。如果你希望等到整個(gè)視圖都渲染

完畢,可以用 vm.$nextTick

vue2.0之后主動(dòng)調(diào)用$destroy()不會(huì)移除dom節(jié)點(diǎn),作者不推薦直接destroy這種做法,如果實(shí)在需要這樣用可以在這個(gè)生命周期鉤子中手動(dòng)移除dom節(jié)點(diǎn)

2. 單個(gè)組件的生命周期

現(xiàn)根據(jù)實(shí)際代碼執(zhí)行情況分析:

<template>
    <div>
        <h3>單組件</h3>
        <el-button @click="dataVar += 1">更新 {{dataVar}}</el-button>
        <el-button @click="handleDestroy">銷毀</el-button>
    </div>
</template>

export default {
    data() {
        return {
            dataVar: 1
        }
    },

    beforeCreate() {
        this.compName = 'single'
        console.log(`--${this.compName}--beforeCreate`)
    },

    created() {
        console.log(`--${this.compName}--created`)
    },

    beforeMount() {
        console.log(`--${this.compName}--beforeMount`)
    },

    mounted() {
        console.log(`--${this.compName}--mounted`)
    },

    beforeUpdate() {
        console.log(`--${this.compName}--beforeUpdate`)
    },

    updated() {
        console.log(`--${this.compName}--updated`)
    },

    beforeDestroy() {
        console.log(`--${this.compName}--beforeDestroy`)
    },

    destroyed() {
        console.log(`--${this.compName}--destroyed`)
    },

    methods: {
        handleDestroy() {
            this.$destroy()
        }
    }
}

初始化組件時(shí),打?。?/p>

當(dāng)data中的值變化時(shí),打?。?/p>

當(dāng)組件銷毀時(shí),打?。?/p>

從打印結(jié)果可以看出:

  • 初始化組件時(shí),僅執(zhí)行了beforeCreate/Created/beforeMount/mounted四個(gè)鉤子函數(shù)

  • 當(dāng)改變data中定義的變量(響應(yīng)式變量)時(shí),會(huì)執(zhí)行beforeUpdate/updated鉤子函數(shù)

  • 當(dāng)切換組件(當(dāng)前組件未緩存)時(shí),會(huì)執(zhí)行beforeDestory/destroyed鉤子函數(shù)

  • 初始化和銷毀時(shí)的生命鉤子函數(shù)均只會(huì)執(zhí)行一次,beforeUpdate/updated可多次執(zhí)行

3. 父子組件的生命周期

將單組件作為基礎(chǔ)組件(由于props在beforeCreate()中未初始化),需要做如下更改:

props: {
    compName: {
        type: String,
        default: 'single'
    }
},

beforeCreate() {
    // this.compName = 'single'
    // console.log(`--${this.compName}--beforeCreate`)

    console.log(` --data未初始化--beforeCreate`)
},

父組件代碼如下:

<template>
    <div class="complex">
        <h3>復(fù)雜組件</h3>
        <lifecycle-single compName="child"></lifecycle-single>
    </div>
</template>


const COMPONENT_NAME = 'complex'

import LifecycleSingle from './LifeCycleSingle'

export default {

    beforeCreate() {
        console.log(`--${COMPONENT_NAME}--beforeCreate`)
    },

    created() {
        console.log(`--${COMPONENT_NAME}--created`)
    },

    beforeMount() {
        console.log(`--${COMPONENT_NAME}--beforeMount`)
    },

    mounted() {
        console.log(`--${COMPONENT_NAME}--mounted`)
    },

    beforeUpdate() {
        console.log(`--${COMPONENT_NAME}--beforeUpdate`)
    },

    updated() {
        console.log(`--${COMPONENT_NAME}--updated`)
    },

    beforeDestroy() {
        console.log(`--${COMPONENT_NAME}--beforeDestroy`)
    },

    destroyed() {
        console.log(`--${COMPONENT_NAME}--destroyed`)
    },

    components: {
        LifecycleSingle
    }
}

初始化組件時(shí),打印:

當(dāng)子組件data中的值變化時(shí),打?。?/p>

當(dāng)父組件data中的值變化時(shí),打印:

當(dāng)props改變時(shí),打?。?/p>

當(dāng)子組件銷毀時(shí),打?。?/p>

當(dāng)父組件銷毀時(shí),打印:

從打印結(jié)果可以看出:

  • 僅當(dāng)子組件完成掛載后,父組件才會(huì)掛載

  • 當(dāng)子組件完成掛載后,父組件會(huì)主動(dòng)執(zhí)行一次beforeUpdate/updated鉤子函數(shù)(僅首次)

  • 父子組件在data變化中是分別監(jiān)控的,但是在更新props中的數(shù)據(jù)是關(guān)聯(lián)的(可實(shí)踐)

  • 銷毀父組件時(shí),先將子組件銷毀后才會(huì)銷毀父組件

4. 兄弟組件的生命周期

在上面的基礎(chǔ)上,復(fù)雜組件做如下更改

<template>
    <div class="complex">
        <h3>復(fù)雜組件</h3>
        <lifecycle-single compName="cihld1"></lifecycle-single>
        <lifecycle-single compName="child2"></lifecycle-single>
        <el-button @click="dataVar += 1">complex更新 {{dataVar}}</el-button>
        <el-button @click="handleDestroy">complex銷毀</el-button>
    </div>
</template>

初始化組件時(shí),打?。?/p>

當(dāng)child1更新和銷毀時(shí),打?。?/p>

當(dāng)child2更新和銷毀時(shí),打印:

當(dāng)父組件銷毀時(shí),打印

從打印結(jié)果可以看出:

  • 組件的初始化(mounted之前)分開進(jìn)行,掛載是從上到下依次進(jìn)行

  • 當(dāng)沒有數(shù)據(jù)關(guān)聯(lián)時(shí),兄弟組件之間的更新和銷毀是互不關(guān)聯(lián)的

5. 宏mixin的生命周期

在上面的基礎(chǔ)上,添加一個(gè)mixin.js文件,內(nèi)容如下:

const COMPONENT_NAME = 'lifecycleMixin'
 
export default {
    name: COMPONENT_NAME,

    beforeCreate() {
        console.log(`--${COMPONENT_NAME}--beforeCreate`)
    },

    created() {
        console.log(`--${COMPONENT_NAME}--created`)
    },

    beforeMount() {
        console.log(`--${COMPONENT_NAME}--beforeMount`)
    },

    mounted() {
        console.log(`--${COMPONENT_NAME}--mounted`)
    },

    beforeUpdate() {
        console.log(`--${COMPONENT_NAME}--beforeUpdate`)
    },

    updated() {
        console.log(`--${COMPONENT_NAME}--updated`)
    },

    beforeDestroy() {
        console.log(`--${COMPONENT_NAME}--beforeDestroy`)
    },

    destroyed() {
        console.log(`--${COMPONENT_NAME}--destroyed`)
    }
}

同樣的,復(fù)雜組件做如下更改:

import lifecycleMixin from './mixin'

export default {

    mixins: [lifecycleMixin],
    // ...
}

組件初始化時(shí),打?。?/p>

組件銷毀時(shí),打?。?/p>

從打印結(jié)果可以看出:

  • mixin中的生命周期與引入該組件的生命周期是僅僅關(guān)聯(lián)的,且mixin的生命周期優(yōu)先執(zhí)行

到此這篇關(guān)于Vue生命周期的文章就介紹到這了。希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

张掖市| 朔州市| 城市| 江达县| 朝阳区| 日喀则市| 呼伦贝尔市| 余姚市| 句容市| 东丰县| 巴林左旗| 滦平县| 剑河县| 休宁县| 尼勒克县| 海伦市| 通山县| 南郑县| 永新县| 松滋市| 嘉峪关市| 秭归县| 梧州市| 邢台县| 玉树县| 图片| 两当县| 手游| 龙川县| 全椒县| 措美县| 潍坊市| 响水县| 饶河县| 峨山| 江北区| 潞城市| 永年县| 吉林市| 乌鲁木齐县| 柯坪县|