Vue技术17.3总结生命周期

<!DOCTYPE html>
<html>
    <head>
        <mata charset="UTF-8" />
        <title>总结生命周期</title>
        <script type="text/javascript" src="../js/vue.js"></script>
    </head>
    <body>
        <!--
            常用的生命周期钩子:
                1.mounted:发送ajax请求、启动定时器、绑定自定义事件、订阅消息等【初始化操作】
                2.beforeDestroy:清除定时器、解绑自定义事件、取消订阅消息等【收尾工作】
            关于销毁Vue实例
                1.销毁后借助Vue开发者工具看不到任何信息。
                2.销毁后自定义事件会失效,但原生DOM事件依然有效。
                3.一般不会在beforeDestroy操作数据,因为即使操作数据,也不会再触发更新流程了。
         -->
        <!-- 准备好一个容器 -->
        <div id="root">
            <h2 :style="{opacity}">欢迎学习Vue</h2>
            <button @click="opacity = 1">透明度设置为1</button>
            <button @click="stop">点我停止变换</button>
        </div>
    </body>

    <script type="text/javascript">
        Vue.config.productionTip = false

        new Vue({
    
    
            el:'#root',
            data:{
    
    
                opacity:1
            },
            methods: {
    
    
                stop(){
    
    
                    this.$destroy()
                }
            },
            mounted(){
    
    
                this.timer = setInterval(() => {
    
    
                    console.log('setInterval')
                    this.opacity -= 0.01
                    if(this.opacity <= 0) this.opacity = 1
                },30)
            },
            beforeDestroy() {
    
    
                clearInterval(this.timer)
            },
        })
    </script>
</html>

猜你喜欢

转载自blog.csdn.net/qq_40713201/article/details/126313595