1. 程式人生 > 其它 >14 vue之生命週期

14 vue之生命週期

1 鉤子函式

create

let vm = new Vue()

mount

掛載,把div掛載到元件中

update

let vm = new Vue({
    el: '#box',
    data: {
        isShow: true    // 修改這個內容
    },
    methods: {
        handleClick() {
            console.log('我是根元件')
        }
    }
})

2 測試程式碼

<!DOCTYPE html>
<html lang="en">
<
head> <meta charset="UTF-8"> <title>生命週期</title> <script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.12/vue.min.js"></script> </head> <body> <div id="box"> <child v-if="isShow"></child> <br> <button @click="terminate"
>刪除子元件</button> <button @click="reborn">顯示子元件</button> </div> </body> <script> Vue.component('child', { template: ` <div> {{name}} <button @click="name='Darker1'">更新資料1</button> <
button @click="name='Darker2'">更新資料2</button> </div>`, data() { return { name: 'Darker1', } }, beforeCreate() { console.group('當前狀態:beforeCreate') console.log('當前el狀態:', this.$el) console.log('當前data狀態:', this.$data) console.log('當前name狀態:', this.name) }, created() { console.group('當前狀態:created') console.log('當前el狀態:', this.$el) console.log('當前data狀態:', this.$data) console.log('當前name狀態:', this.name) }, beforeMount() { console.group('當前狀態:beforeMount') console.log('當前el狀態:', this.$el) console.log('當前data狀態:', this.$data) console.log('當前name狀態:', this.name) }, mounted() { console.group('當前狀態:mounted') console.log('當前el狀態:', this.$el) console.log('當前data狀態:', this.$data) console.log('當前name狀態:', this.name) //用的最多,向後端載入資料,建立定時器等 console.log("頁面已被vue例項渲染, data, methods已更新"); console.log('mounted') this.t = setInterval(function () { console.log('daada') }, 3000) }, beforeUpdate() { console.group('當前狀態:beforeUpdate') console.log('當前el狀態:', this.$el) console.log('當前data狀態:', this.$data) console.log('當前name狀態:', this.name) }, updated() { console.group('當前狀態:updated') console.log('當前el狀態:', this.$el) console.log('當前data狀態:', this.$data) console.log('當前name狀態:', this.name) }, beforeDestroy() { console.group('當前狀態:beforeDestroy') console.log('當前el狀態:', this.$el) console.log('當前data狀態:', this.$data) console.log('當前name狀態:', this.name) }, destroyed() { console.group('當前狀態:destroyed') console.log('當前el狀態:', this.$el) console.log('當前data狀態:', this.$data) console.log('當前name狀態:', this.name) //元件銷燬,清理定時器 clearInterval(this.t) this.t = null console.log('destoryed') }, }) let vm = new Vue({ el: '#box', data: { isShow: true }, methods: { terminate() { this.isShow = false }, reborn() { this.isShow = true } } }) </script> </html>

課堂案列:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>生命週期</title>
    <script src="./js/vue.js"></script>
</head>
<body>
<div id="box">
    <child v-if="isShow"></child>
    <hr>

    <button @click="terminate">刪除子元件</button>
    <button @click="reborn">顯示子元件</button>
</div>
</body>
<script>
    // 子元件   child  定義的元件,相當於一個標籤,但是他有自己的模板和樣式,事件
    Vue.component('child', {
        template: `
                <div>
                    <button @click="handleClick">點我,name更新</button>
                    <br>
                    {{name}}
                </div>

            `,

        data() {
            return {
                name: 'lqz',
                t:''
            }
        },
        methods:{
            handleClick(){
                this.name='彭于晏'
            }
    },
        beforeCreate() {
            console.group('當前狀態:beforeCreate')
            console.log('當前el狀態:', this.$el)
            console.log('當前data狀態:', this.$data)
            console.log('當前name狀態:', this.name)
        },
        created() {
            console.group('當前狀態:created')
            console.log('當前el狀態:', this.$el)
            console.log('當前data狀態:', this.$data)
            console.log('當前name狀態:', this.name)
        },
        beforeMount() {
            console.group('當前狀態:beforeMount')
            console.log('當前el狀態:', this.$el)
            console.log('當前data狀態:', this.$data)
            console.log('當前name狀態:', this.name)
        },
        mounted() {
            console.group('當前狀態:mounted')
            console.log('當前el狀態:', this.$el)
            console.log('當前data狀態:', this.$data)
            console.log('當前name狀態:', this.name)
          //用的最多,向後端載入資料,建立定時器等
            console.log("頁面已被vue例項渲染, data, methods已更新");
            console.log('mounted')
            // 定時器,每隔3s鍾,列印daada
            this.t = setInterval(function () {
                console.log('daada')
            }, 3000)

        },
        beforeUpdate() {
            console.group('當前狀態:beforeUpdate')
            console.log('當前el狀態:', this.$el)
            console.log('當前data狀態:', this.$data)
            console.log('當前name狀態:', this.name)
        },
        updated() {
            console.group('當前狀態:updated')
            console.log('當前el狀態:', this.$el)
            console.log('當前data狀態:', this.$data)
            console.log('當前name狀態:', this.name)
        },
        beforeDestroy() {
            console.group('當前狀態:beforeDestroy')
            console.log('當前el狀態:', this.$el)
            console.log('當前data狀態:', this.$data)
            console.log('當前name狀態:', this.name)
        },
        destroyed() {
            console.group('當前狀態:destroyed')
            console.log('當前el狀態:', this.$el)
            console.log('當前data狀態:', this.$data)
            console.log('當前name狀態:', this.name)
          //元件銷燬,清理定時器
            clearInterval(this.t)
            this.t = null
            console.log('destoryed')
        },


    })



    let vm = new Vue({
        el: '#box',
        data: {
            isShow: true
        },
        methods: {
            terminate() {
                this.isShow = false
            },
            reborn() {
                this.isShow = true
            }
        },

    })







</script>
</html>