欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Vue中的生命周期介紹

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

什么是vue的生命周期

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

從圖片中可以看出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沒(méi)有
                    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("模板編譯之前,還沒(méi)掛載");
                },
                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ì)有渲染頁(yè)面,才能看到頁(yè)面上數(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)文章

  • vue3中update:modelValue的使用與不生效問(wèn)題解決

    vue3中update:modelValue的使用與不生效問(wèn)題解決

    現(xiàn)在vue3的使用越來(lái)越普遍了,vue3這方面的學(xué)習(xí)我們要趕上,下面這篇文章主要給大家介紹了關(guān)于vue3中update:modelValue的使用與不生效問(wèn)題的解決方法,需要的朋友可以參考下
    2022-03-03
  • Vue聲明式導(dǎo)航與編程式導(dǎo)航及導(dǎo)航守衛(wèi)和axios攔截器全面詳細(xì)講解

    Vue聲明式導(dǎo)航與編程式導(dǎo)航及導(dǎo)航守衛(wèi)和axios攔截器全面詳細(xì)講解

    這篇文章主要介紹了Vue聲明式導(dǎo)航與編程式導(dǎo)航及導(dǎo)航守衛(wèi)和axios攔截器,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧
    2023-01-01
  • Vue中data數(shù)據(jù)初始化方法詳解

    Vue中data數(shù)據(jù)初始化方法詳解

    這篇文章主要介紹了Vue中data數(shù)據(jù)初始化方法,數(shù)據(jù)初始化是在組件實(shí)例化時(shí)發(fā)生的,在組件中,可以通過(guò)data選項(xiàng)來(lái)定義組件的初始數(shù)據(jù),需要詳細(xì)了解可以參考下文
    2023-05-05
  • 在vue中使用echarts的方法以及可能遇到的問(wèn)題

    在vue中使用echarts的方法以及可能遇到的問(wèn)題

    Echarts是一個(gè)與框架無(wú)關(guān)的JS圖表庫(kù),但是它基于Js,這樣很多框架都能使用它,下面這篇文章主要給大家介紹了關(guān)于在vue中使用echarts的方法以及可能遇到的問(wèn)題的相關(guān)資料,需要的朋友可以參考下
    2022-09-09
  • Vue.js路由vue-router使用方法詳解

    Vue.js路由vue-router使用方法詳解

    這篇文章主要為大家詳細(xì)介紹了Vue.js路由vue-router使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-03-03
  • vue使用websocket的方法實(shí)例分析

    vue使用websocket的方法實(shí)例分析

    這篇文章主要介紹了vue使用websocket的方法,結(jié)合實(shí)例形式對(duì)比分析了vue.js使用websocket的相關(guān)操作技巧與注意事項(xiàng),需要的朋友可以參考下
    2019-06-06
  • vue父組件調(diào)用子組件方法報(bào)錯(cuò)問(wèn)題及解決

    vue父組件調(diào)用子組件方法報(bào)錯(cuò)問(wèn)題及解決

    這篇文章主要介紹了vue父組件調(diào)用子組件方法報(bào)錯(cuò)問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-09-09
  • vue如何通過(guò)params和query傳值(刷新不丟失)

    vue如何通過(guò)params和query傳值(刷新不丟失)

    這篇文章主要介紹了vue如何通過(guò)params和query傳值(刷新不丟失),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-04-04
  • Vue實(shí)現(xiàn)雙向數(shù)據(jù)綁定

    Vue實(shí)現(xiàn)雙向數(shù)據(jù)綁定

    這篇文章主要為大家詳細(xì)介紹了Vue實(shí)現(xiàn)雙向數(shù)據(jù)綁定的方式,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-05-05
  • Vue3中vue-router的使用方法詳解

    Vue3中vue-router的使用方法詳解

    Vue?Router?是?Vue?的官方路由,它與?Vue.js?核心深度集成,讓用?Vue.js?構(gòu)建單頁(yè)應(yīng)用變得輕而易舉,本文將通過(guò)簡(jiǎn)單的示例為大家介紹一下vue-router的使用,需要的可以參考一下
    2023-06-06

最新評(píng)論