Vue生命周期

 Vue生命周期图

例子: 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div id="root">
    <!--    <h2>当前n的值是:<span v-text="n"></span></h2>-->
    <!--    <button @click="add">点我n++</button>-->
    <!--    <button @click="bye">点击销毁vm</button>-->
</div>

<script src="./js/vue.js"></script>
<script>
    Vue.config.productionTip = false;
    let vm = new Vue({
        el: '#root',
        template: `
          <div>
          <h2>当前n的值是:<span v-text="n"></span></h2>
          <button @click="add">点我n++</button>
          <button @click="bye">点击销毁vm</button>
          </div>`,
        data() {
            return {
                n: 1
            }
        }, methods: {
            add() {
                console.log("执行了Add函数");
                this.n++;
            },
            bye() {
                console.log("执行了销毁函数");
                this.$destroy();
            }
        }, watch: {
            n() {
                console.log("n变了");
            }
        },
        beforeCreate() {
            console.log("beforeCreate");
            console.log(this.n);
        },
        created() {
            console.log("created");
            console.log(this.n);
        }, beforeMount() {
            console.log("beforeMount");
        },mounted(){
            console.log("mounted");
        },beforeUpdate(){
            console.log("beforeUpdate");
        },
        updated(){
            console.log("updated");
        },
        beforeDestroy(){
            console.log("beforeDestroy");
        },
        destroyed(){
            console.log("destroyed");
        }
    });
</script>
</body>
</html>

 我们可以很清楚的看到每一个生命周期点是什么时候执行得

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值