Vue入門九、Vue生命周期

先上圖:
Vue入門九、Vue生命周期

成都創(chuàng)新互聯(lián)公司主要從事成都網(wǎng)站設(shè)計、做網(wǎng)站、網(wǎng)頁設(shè)計、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)黃陵,十年網(wǎng)站建設(shè)經(jīng)驗,價格優(yōu)惠、服務(wù)專業(yè),歡迎來電咨詢建站服務(wù):13518219792

示例代碼:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<script type="text/javascript" src="vue.js"></script>
<div id="app"></div>
<script type="text/javascript">
    var mytest = {
        template: `
                <div>測試 {{msg}}
                    <br>
                    <button @click="msg+='1'" >點一下數(shù)據(jù)會更新</button>
                </div>
            `,
        data() {
            return {
                msg: '嗯呢'
            }
        },

        // 組件創(chuàng)建前
        beforeCreate() {
            console.log('組件創(chuàng)建前')
            console.log(this.msg)
        },
        // 組件創(chuàng)建后
        created() {
            console.log('組件創(chuàng)建后')
            console.log(this.msg)
        },
        // Dom 掛載前
        beforeMount() {
            console.log('Dom掛載前')
            console.log(document.body.innerText)
        },
        // Dom 掛載后
        mounted() {
            console.log('Dom掛載后')
            console.log(document.body.innerText)
        },
        // 數(shù)據(jù)變更前
        beforeUpdate() {
            console.log('數(shù)據(jù)更新前')
            console.log(document.body.innerText)
        },
        // 數(shù)據(jù)變更后
        updated() {
            console.log('數(shù)據(jù)更新后')
            console.log(document.body.innerText)
        },
        // 組件銷毀前
        beforeDestroy() {
            console.log('組件銷毀前')
        },
        // 組件銷毀后
        destroyed() {
            console.log('組件銷毀后')
        },
        // 組件激活
        activated() {
            console.log('組件激活')
        },
        // 組件停用
        deactivated() {
            console.log('組件停用')
        }
    }
    new Vue({
        el: '#app',
        template: `
                <div>
                    <keep-alive><mytest v-if="mytestShow"></mytest></keep-alive>
                    <button @click="clickDestroy">組件銷毀</button>
</div>
            `,
        components: {
            mytest
        },
        data() {
            return {
                mytestShow: true
            }
        },
        methods: {
            clickDestroy() {
                this.mytestShow = !this.mytestShow
            }
        }
    })

</script>
</body>
</html>

在需要頻繁的創(chuàng)建和銷毀組件,如果用的是v-if,可以使用activated()deactivated()對組件進(jìn)行激活和停用,前提是被操作組件要用<keep alive></keep alive>包裹
例:<keep-alive><mytest v-if="mytestShow"></mytest></keep-alive>

網(wǎng)站題目:Vue入門九、Vue生命周期
文章分享:http://www.muchs.cn/article10/jidodo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站制作微信小程序、用戶體驗、軟件開發(fā)、網(wǎng)站策劃網(wǎng)站設(shè)計

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)