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

Vue中的生命周期介紹

 更新時(shí)間:2022年03月14日 10:28:27   作者:.NET開發(fā)菜鳥  
這篇文章介紹了Vue中的生命周期,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

什么是vue的生命周期

Vue中的生命周期是指組件從創(chuàng)建到銷毀的一系列過程??聪旅孢@張官方文檔的圖:

從圖片中可以看出Vue的整個(gè)生命周期包括8個(gè)狀態(tài),按照先后順序分別為:

  • beforeCreate
  • Created
  • beforeMount
  • mounted
  • beforeUpdate
  • updated
  • beforeDestroy
  • destroyed

Vue組件的生命周期共分為三個(gè)階段,如下圖所示:

創(chuàng)建階段和銷毀階段在組件的生命周期中只會(huì)執(zhí)行一次,而更新階段會(huì)執(zhí)行多次。

先看一下創(chuàng)建階段完成的事情:

在看更新階段完成的事情:

最后在看一下銷毀階段完成的事情:

先看下面的一段代碼:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>生命周期</title>
    <!--引入vue.js-->
    <script src="./js/vue.js" ></script>
    <script>
        window.onload=function(){
            new Vue({
                el:'#app',// 2.0不允許掛載到html,body元素上
                data:{
                    msg:'welcome'
                },
                methods:{
                    update(){
                        this.msg="歡迎";
                    },
                    destroy(){
                        this.$destroy();
                    }
                },
                //創(chuàng)建前狀態(tài)  el和data并未初始化
                beforeCreate(){
                    console.group('------beforeCreate創(chuàng)建前狀態(tài)------');
                    console.log("%c%s", "color:red" , "el     : " + this.$el); //undefined
                    console.log("%c%s", "color:red","data   : " + this.$data); //undefined 
                    console.log("%c%s", "color:red","message: " + this.msg) 
                    console.log('組件實(shí)例剛剛創(chuàng)建,還未進(jìn)行數(shù)據(jù)觀測(cè)和事件配置');
                },
                created(){//常用  創(chuàng)建完畢狀態(tài)   完成了data數(shù)據(jù)的初始化  el沒有
                    console.group('------created創(chuàng)建完畢狀態(tài)------');
                    console.log("%c%s", "color:red","el     : " + this.$el); //undefined
                    console.log("%c%s", "color:red","data   : " + this.$data); //已被初始化 
                    console.log("%c%s", "color:red","message: " + this.msg); //已被初始化
                    console.log("實(shí)例已經(jīng)創(chuàng)建完成,并且已經(jīng)進(jìn)行數(shù)據(jù)觀測(cè)和事件配置")
                },
                beforeMount(){  //掛載前狀態(tài) 完成了el和data初始化
                    this.msg="112233";
                    console.group('------beforeMount掛載前狀態(tài)------');
                    console.log("%c%s", "color:red","el     : " + (this.$el)); //已被初始化
                    console.log(this.$el);
                    console.log("%c%s", "color:red","data   : " + this.$data); //已被初始化  
                    console.log("%c%s", "color:red","message: " + this.msg); //已被初始化
                    console.log("模板編譯之前,還沒掛載");
                },
                mounted(){//常用  掛載結(jié)束狀態(tài)  完成掛載
                    console.group('------mounted 掛載結(jié)束狀態(tài)------');
                    console.log("%c%s", "color:red","el     : " + this.$el); //已被初始化
                    console.log(this.$el);    
                    console.log("%c%s", "color:red","data   : " + this.$data); //已被初始化
                    console.log("%c%s", "color:red","message: " + this.msg); //已被初始化 
                    console.log("模板編譯之后,已經(jīng)掛載,此時(shí)才會(huì)有渲染頁面,才能看到頁面上數(shù)據(jù)的顯示")
                },
                beforeUpdate(){   //更新前狀態(tài)
                    console.group('------beforeUpdate 更新前狀態(tài)------');
                    console.log("%c%s", "color:red","el     : " + this.$el);
                    console.log(this.$el);   
                    console.log("%c%s", "color:red","data   : " + this.$data); 
                    console.log("%c%s", "color:red","message: " + this.msg); 
                },
                updated(){   //更新完成狀態(tài)
                    console.group('------updated 更新完成狀態(tài)------');
                    console.log("%c%s", "color:red","el     : " + this.$el);
                    console.log(this.$el); 
                    console.log("%c%s", "color:red","data   : " + this.$data); 
                    console.log("%c%s", "color:red","message: " + this.msg); 
                },
                beforeDestroy(){   //銷毀前狀態(tài)
                    console.group('------beforeDestroy 銷毀前狀態(tài)------');
                    console.log("%c%s", "color:red","el     : " + this.$el);
                    console.log(this.$el);    
                    console.log("%c%s", "color:red","data   : " + this.$data); 
                    console.log("%c%s", "color:red","message: " + this.msg); 
                },
                destroyed(){  //銷毀完成狀態(tài)
                    console.group('------destroyed 組件銷毀完成狀態(tài)------');
                    console.log("%c%s", "color:red","el     : " + this.$el);
                    console.log(this.$el);  
                    console.log("%c%s", "color:red","data   : " + this.$data); 
                    console.log("%c%s", "color:red","message: " + this.msg)
                }
            });
        }
    </script>
</head>
<body>
    <div id="app">
        <input type="text" v-model="msg" />
        <button @click="update">更新數(shù)據(jù)</button>
        <button @click="destroy">銷毀組件</button> 
    </div>
</body>
</html>

在控制臺(tái)的console里面查看運(yùn)行后的效果:

然后點(diǎn)擊“更新數(shù)據(jù)”按鈕,會(huì)看到input綁定的數(shù)據(jù)發(fā)生變化:

數(shù)據(jù)更新前:

數(shù)據(jù)更新后:

控制臺(tái)顯示的打印信息:

最后點(diǎn)擊“銷毀組件”按鈕,查看控制臺(tái)顯示的打印信息:

這樣,一個(gè)完整的Vue實(shí)例生命周期就結(jié)束了。

注意:Vue組件被銷毀以后,這時(shí)如果在更新數(shù)據(jù)就不會(huì)有任何反應(yīng)了,因?yàn)榻M件已經(jīng)被銷毀

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

相關(guān)文章

  • vue實(shí)現(xiàn)一個(gè)單獨(dú)的組件注釋

    vue實(shí)現(xiàn)一個(gè)單獨(dú)的組件注釋

    這篇文章主要介紹了vue實(shí)現(xiàn)一個(gè)單獨(dú)的組件注釋,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-04-04
  • Vue3+Ant?design?實(shí)現(xiàn)Select下拉框一鍵全選/清空功能

    Vue3+Ant?design?實(shí)現(xiàn)Select下拉框一鍵全選/清空功能

    在做后臺(tái)管理系統(tǒng)項(xiàng)目的時(shí)候,產(chǎn)品增加了一個(gè)在Select選擇器中添加一鍵全選和清空的功能,他又不讓在外部增加按鈕,其實(shí)如果說在外部增加按鈕實(shí)現(xiàn)全選或者清空的話,功能比較簡(jiǎn)單的,下面給大家分享Vue3+Ant?design?實(shí)現(xiàn)Select下拉框一鍵全選/清空功能,需要的朋友可以參考下
    2024-05-05
  • vue獲取DOM元素并設(shè)置屬性的兩種實(shí)現(xiàn)方法

    vue獲取DOM元素并設(shè)置屬性的兩種實(shí)現(xiàn)方法

    下面小編就為大家?guī)硪黄獀ue獲取DOM元素并設(shè)置屬性的兩種實(shí)現(xiàn)方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-09-09
  • 詳解Vue的鉤子函數(shù)(路由導(dǎo)航守衛(wèi)、keep-alive、生命周期鉤子)

    詳解Vue的鉤子函數(shù)(路由導(dǎo)航守衛(wèi)、keep-alive、生命周期鉤子)

    這篇文章主要介紹了詳解Vue的鉤子函數(shù)(路由導(dǎo)航守衛(wèi)、keep-alive、生命周期鉤子),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-07-07
  • vue項(xiàng)目中使用lib-flexible解決移動(dòng)端適配的問題解決

    vue項(xiàng)目中使用lib-flexible解決移動(dòng)端適配的問題解決

    這篇文章主要介紹了vue項(xiàng)目中使用lib-flexible解決移動(dòng)端適配的問題解決,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-08-08
  • Element DateTimePicker日期時(shí)間選擇器的使用示例

    Element DateTimePicker日期時(shí)間選擇器的使用示例

    這篇文章主要介紹了Element DateTimePicker日期時(shí)間選擇器的使用示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07
  • 如何封裝Vue Element的table表格組件

    如何封裝Vue Element的table表格組件

    這篇文章主要介紹了如何封裝Vue Element的table表格組件,幫助大家更好的理解和使用vue框架,感興趣的朋友可以了解下
    2021-02-02
  • 關(guān)于Vue中過濾器的必懂小知識(shí)

    關(guān)于Vue中過濾器的必懂小知識(shí)

    vue過濾器可以在不改變?cè)紨?shù)據(jù),只是對(duì)數(shù)據(jù)進(jìn)行加工處理后返回過濾后的數(shù)據(jù)再進(jìn)行調(diào)用處理,下面這篇文章主要給大家介紹了關(guān)于Vue中過濾器必懂小知識(shí)的相關(guān)資料,需要的朋友可以參考下
    2021-10-10
  • vue3中的ref,toRef,toRefs三個(gè)的作用使用小結(jié)

    vue3中的ref,toRef,toRefs三個(gè)的作用使用小結(jié)

    Vue3中ref、reactive、toRef、toRefs都是與響應(yīng)式數(shù)據(jù)相關(guān)的,就此做一份筆記作為區(qū)別,本文重點(diǎn)給大家講解vue3中的ref,toRef,toRefs三個(gè)是干嘛的,有什么作用,感興趣的朋友跟隨小編一起看看吧
    2022-11-11
  • vue中Form 表單的 resetFields() 失效原因及問題解決

    vue中Form 表單的 resetFields() 失效原因及問題解決

    在Vue項(xiàng)目中,使用formRef.value.resetFields()方法重置表單時(shí)可能遇到不起作用的問題,下面就來介紹一下如何解決,感興趣的可以了解一下
    2024-09-09

最新評(píng)論

襄樊市| 巴东县| 巫山县| 桃园市| 新平| 八宿县| 彩票| 环江| 新绛县| 福安市| 廉江市| 荔浦县| 夏津县| 宿松县| 潍坊市| 缙云县| 泰来县| 原平市| 罗甸县| 盐津县| 博爱县| 徐水县| 平顶山市| 沈阳市| 乐都县| 麻栗坡县| 会理县| 德化县| 徐水县| 彭泽县| 禄丰县| 鹰潭市| 开江县| 山阴县| 那曲县| 盐山县| 都江堰市| 新绛县| 淮滨县| 剑河县| 商都县|