Vue 生命周期

Vue生命周期

  • 生命周期又名生命周期回调函数, 生命周期函数, 生命周期钩子
  • 是什么: Vue 在关键时刻帮我们调用的一下特殊名称函数
  • 生命周期函数的名字不可改变, 但函数的具体内容是程序员根据需求编写
  • 生命周期函数中的 this 指向 vm 或 组件实例对象

图解(重要)

在这里插入图片描述

代码示例

<body>
    <div id="root" x="2">
        <h1>{{n}}</h1>
        <button @click="add_ticket">加一</button>
        <button @click="buy">销毁vm</button>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
    <script src="https://unpkg.com/axios/dist/axios.min.js"></script>

    <script type="text/javascript">

        var vm = new Vue({
            el: "#root",
            data: {
                n: 1
            },
            methods: {
                add_ticket(){
                    console.log("add")
                    this.n++
                },
                buy(){
                    console.log("buy调用")
                    this.$destroy();
                }
            },

            beforeCreate(){
                console.log("beforeCreate调用")
                console.log(this.n)
                console.log(this)
            },
            created(){
                console.log("creaate调用")
                console.log(this.n)
            },
            beforeMount() {
                console.log("beforeMount调用")
                console.log(this.n)
            },
            mounted(){
                console.log("mounted调用")
                console.log(this.n)
            },
            beforeUpdate(){
                console.log("beforeUpdate调用")
                console.log(this.n)
            },
            updated(){
                console.log("updated调用")
                console.log(this.n)
            },
            beforeDestroy(){
                console.log("beforeDestroy调用")
                console.log(this.n)
            },
            destroyed(){
                console.log("destroyed调用")
                console.log(this.n)
                console.log(this)
            }
        });

    </script>
</body>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值