17.生命周期

59 阅读1分钟

1.引出生命周期

<div id="root">
    <h1 v-if="a">juejin</h1>
    <h1 :style="{opacity}">欢迎来掘金</h1>
</div>

 <script>
 
        new Vue({
        el: '#root',
        data: {
            a: false,
            opacity: 1
        },
        //Vue完成模板的解析并把初始的真实DOM元素放入页面(挂载完毕)调用mounted
        mounted() {
            console.log('mounted')
            setInterval(() => {
                this.opacity -= 0.01
                if (this.opacity <= 0) this.opacity = 1

            }, 16);
        },
    })

    // 通过外部的定时器实现(不推荐)
    // setInterval(() => {
    //     vm.opacity -= 0.01
    //     if (vm.opacity <= 0) vm.opacity = 1
    // }, 16);
 </script>

字体透明度由1逐渐减小为0再变为1
image.png

小结
image.png

2.分析生命周期

image.png

image.png

image.png

1).挂载流程

   <div id="root" :x="n">
    <h1>当前n的值为{{n}}</h1>
    <button @click="add">点我n+1</button>
</div>
<script>
    new Vue({
        el: '#root',
    //     template: `
    // <div>
    //     <h1>当前n的值为{{n}}</h1>
    //     <button @click="add">点我n+1</button>
    // </div>
    //     `,
        data: {
            n: 1
        },
        methods: {
            add() {
                this.n++
            }
        },
        beforeCreate() {
            console.log('beforeCreate')
            // console.log(this)
            // debugger
        },
        created() {
            console.log('created')
            // console.log(this)
            // debugger
        },
        beforeMount() {
            console.log('beforeMount')
            // console.log(this)
            // document.querySelector('h1').innerText = 123
            // debugger
        },
        mounted() {
            console.log('mounted')
            // console.log(this)
            // document.querySelector('h1').innerText = 123
            // debugger
        },

    })

</script>

分析画圈这小段
image.png 按下方图片改变一下代码
image.png

效果如图
image.png 在控制台输入vm.$mount('#root')
image.png

分析画圈这小段
image.png 按下方图片改变一下代码
image.png

效果如图
image.png

2).更新流程
image.png

image.png

image.png

image.png

image.png

3).销毁流程
image.png

  <div id="root" :x="n">
    <h1 v-text="n"></h1>
    <h1>当前n的值为{{n}}</h1>
    <button @click="add">点我n+1</button>
    <button @click="bye">点我销毁vm</button>
</div>
<script>
    new Vue({
        el: '#root',
        //     template: `
        // <div>
        //     <h1>当前n的值为{{n}}</h1>
        //     <button @click="add">点我n+1</button>
        // </div>
        //     `,
        data: {
            n: 1
        },
        methods: {
            add() {
                console.log('add')
                this.n++
            },
            bye() {
                console.log('bye')
                this.$destroy()
            }
        },
        watch: {
            n() {
                console.log('n变了')
            }

        },
        beforeCreate() {
            console.log('beforeCreate')
            // console.log(this)
            // debugger
        },
        created() {
            console.log('created')
            // console.log(this)
            // debugger
        },
        beforeMount() {
            console.log('beforeMount')
            // console.log(this)
            // document.querySelector('h1').innerText = 123
            // debugger
        },
        mounted() {
            console.log('mounted')
            // console.log(this)
            // document.querySelector('h1').innerText = 123
            // debugger
        },
        beforeUpdate() {
            console.log('beforeupdate')
            // console.log(this.n)
            // debugger
        },
        updated() {
            console.log('updated')
            // console.log(this.n)
            this.n = 99
            // debugger 
        },
        beforeDestroy() {
            console.log('beforeDestroy')
            // this.n = 99
            this.add()
            console.log(this.n)
        },
        destroyed() {
            console.log('destroyed')
        },

    })

</script>

image.png

3.生命周期的总结

image.png

 <div id="root">
    <h1 v-if="a">juejin</h1>
    <h1 :style="{opacity}">欢迎来掘金</h1>
    <button @click="stop">点我停止变化</button>
    <button @click="opacity=1">点我透明度变为1</button>
</div>
<script>
    new Vue({
        el: '#root',
        data: {
            a: false,
            opacity: 1
        },
        methods: {
            stop() {
                // clearInterval(this.timer)
                this.$destroy()
            }
        },
        //Vue完成模板的解析并把初始的真实DOM元素放入页面(挂载完毕)调用mounted
        mounted() {
            console.log('mounted')
            this.timer = setInterval(() => {
                console.log('timer')
                this.opacity -= 0.01
                if (this.opacity <= 0) this.opacity = 1

            }, 16);
        },
        beforeDestroy() {
            console.log('vm即将驾鹤西游了')
            clearInterval(this.timer)
        },
    })
</script>

image.png 总结
image.png