Vue钩子生命周期函数

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <div id="app">
        {{msg}} <br>
        <button @click="destroy">Destroy</button>
    </div>
</body>
<script src="./js/vue.js"></script>
<script>
    Vue.config.productionTip=false;
    new Vue({
        el:"#app",
        data:{
            msg:"hello vue666",
            timer:""
        },
        methods:{
            destroy(){this.$destroy()}
        },

        //初始化阶段
        beforeCreate(){
           //Vue刚刚开始初始化;事件相关的机制已经比较完整了;可以注册事件并触发事件的
           /*this.$on("test",(flag)=>{console.log("test",flag)});
           this.$emit("test",123)*/

            //在beforeCreate钩子中只完成了自定义事件的初始化
            //数据的转绑 数据的响应式都还没有开始执行
            this.msg = "xxx"
        },
        created(){
            //完成了数据的转绑 数据的响应式
            // this.msg = "xxx";
            //最早可以发送ajax请求的地方;
            //在created钩子中所有的挂载相关的工作都还没有开始执行
            //不能进行dom操作;不能相信dom;因为dom还没有被渲染
        },
        beforeMount(){
            //挂载前
            console.log("beforeMount",this.$el);
        },
        mounted(){
            //挂载后 可以进行UI效果的编写
            console.log("mounted",this.$el);
            this.timer = setInterval(()=>{
                console.log("setInterval")
                this.msg+="6"
            },1000)
        },

        //响应式阶段
        beforeUpdate(){
            //响应式前
            console.log("beforeUpdate")
        },
        updated(){
            //响应式后
            console.log("updated")
        },

        //死亡阶段
        beforeDestroy(){console.log("beforeDestroy")},
        destroyed(){
            //收尾!!!
            clearInterval(this.timer)
            console.log("destroyed")
        }
    })
</script>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值