VUE 之 生命周期

1、

   Vue实例的生命周期分为8个周期

  1.1

    beforeCreate:在实例创建前

<div id="app">
        {{ name }}
        <button @click="myClick">点击修改数据</button>
    </div>

    <script>
        new Vue({
            el: "#app",
            data: {
                name: "maweihua",
            },
            methods: {
                init: function () {
                    console.log(this.name);
                },
                myClick: function () {
                    this.name = "Pizza";
                }
            },
            beforeCreate(){
                console.group("beforeCreate");                                            # console.group()将打印的内容放在一个组里
                console.log("el: ", this.$el);
                console.log("data: ", this.$data);
                console.log("name: ", this.name);
                console.log("init: ", this.init);
                console.log("innerHTML: ", document.getElementById("app").innerHTML);
            },
        })
    </script>

  1.2、created  创建之后

<script>
        new Vue({
            el: "#app",
            data: {
                name: "maweihua",
            },
            methods: {
                init: function () {
                    console.log(this.name);
                },
                myClick: function () {
                    this.name = "Pizza";
                }
            },
            created(){
                console.group("beforeCreate");
                console.log("el: ", this.$el);
                console.log("data: ", this.$data);
                console.log("name: ", this.name);
                console.log("init: ", this.init);
                console.log("innerHTML: ", document.getElementById("app").innerHTML);
            },
        })
    </script>

  1.3 beforeMount   挂载之前

<script>
        new Vue({
            el: "#app",
            data: {
                name: "maweihua",
            },
            methods: {
                init: function () {
                    console.log(this.name);
                },
                myClick: function () {
                    this.name = "Pizza";
                }
            },
            beforeMount(){
                console.group("beforeCreate");
                console.log("el: ", this.$el);
                console.log("data: ", this.$data);
                console.log("name: ", this.name);
                console.log("init: ", this.init);
                console.log("innerHTML: ", document.getElementById("app").innerHTML);
            },
        })
    </script>

  1.4 mounted 挂载之后

<script>
        new Vue({
            el: "#app",
            data: {
                name: "maweihua",
            },
            methods: {
                init: function () {
                    console.log(this.name);
                },
                myClick: function () {
                    this.name = "Pizza";
                }
            },
            mounted(){
                console.group("beforeCreate");
                console.log("el: ", this.$el);
                console.log("data: ", this.$data);
                console.log("name: ", this.name);
                console.log("init: ", this.init);
                console.log("innerHTML: ", document.getElementById("app").innerHTML);
            },
        })
    </script>

  1.5 beforeUpdate  更新之前

 <script>
        new Vue({
            el: "#app",
            data: {
                name: "maweihua",
            },
            methods: {
                init: function () {
                    console.log(this.name);
                },
                myClick: function () {
                    this.name = "Pizza";
                }
            },
            beforeUpdate(){
                console.group("beforeCreate");
                console.log("el: ", this.$el);
                console.log("data: ", this.$data);
                console.log("name: ", this.name);
                console.log("init: ", this.init);
                console.log("innerHTML: ", document.getElementById("app").innerHTML);
            },
        })
    </script>

 

 

   1.6 updated 更新之后

 <script>
        new Vue({
            el: "#app",
            data: {
                name: "maweihua",
            },
            methods: {
                init: function () {
                    console.log(this.name);
                },
                myClick: function () {
                    this.name = "Pizza";
                }
            },
            updated(){
                console.group("beforeCreate");
                console.log("el: ", this.$el);
                console.log("data: ", this.$data);
                console.log("name: ", this.name);
                console.log("init: ", this.init);
                console.log("innerHTML: ", document.getElementById("app").innerHTML);
            },
        })
    </script>

  1.7 beforeDestory  销毁之前

 

<div id="app">

    </div>

    <script>
        let Laside = {
            template: `
                <div>
                    <h1>{{ mes }}</h1>

                </div>
            `,
            data () {
                return {
                    mes: "Hello Vue!"
                }
            },
            methods: {
                changeData: function () {
                    this.mes = "Pizza is here!";
                }
            },

            // 组件的创建和销毁对性能有影响
            beforeDestroy() {
                console.log("beforeDestroy");
                console.log("el: ", this.$el);
                console.log("data: ", this.$data);
                console.log("name: ", this.mes);
            },
        };

        let App = {
            template: `
                <div >
                    <Laside v-if="isShow"></Laside>
                    <button @click="showHide">创建消除组件</button>
                </div>
            `,
            // 判断有没有嵌套的子组件
            components: {
                "Laside": Laside,
            },
            methods: {
                showHide: function () {
                    this.isShow = !this.isShow;
                }
            },
            data () {
                return {
                    isShow: true,
                }
            }
        };

        new Vue({
            el: "#app",
            template: `<App/>`,
            components: {
                App,
            }
        })
    </script>

点击按钮才会触发

  1.8 destoryed

<div id="app">

    </div>

    <script>
        let Laside = {
            template: `
                <div>
                    <h1>{{ mes }}</h1>

                </div>
            `,
            data () {
                return {
                    mes: "Hello Vue!"
                }
            },
            methods: {
                changeData: function () {
                    this.mes = "Pizza is here!";
                }
            },

            // 组件的创建和销毁对性能有影响
            destroyed() {
                console.log("beforeDestroy");
                console.log("el: ", this.$el);
                console.log("data: ", this.$data);
                console.log("name: ", this.mes);
            },
        };

        let App = {
            template: `
                <div >
                    <Laside v-if="isShow"></Laside>
                    <button @click="showHide">创建消除组件</button>
                </div>
            `,
            // 判断有没有嵌套的子组件
            components: {
                "Laside": Laside,
            },
            methods: {
                showHide: function () {
                    this.isShow = !this.isShow;
                }
            },
            data () {
                return {
                    isShow: true,
                }
            }
        };

        new Vue({
            el: "#app",
            template: `<App/>`,
            components: {
                App,
            }
        })
    </script>

    点击按钮才会触发。

    由于destoryed是将数据销毁,然后重新加载整的DOM树,所以有了activated,用来将数据存储在缓存中。

  1.9 activated 

 

转载于:https://www.cnblogs.com/wf123/p/9935450.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值