• 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 

  • 相关阅读:
    C语言博客作业-字符数组
    C语言博客作业--一二维数组
    个人作业5
    个人作业4
    个人作业3
    201521123072 结对编程
    软件工程 个人阅读作业2
    软件工程 个人阅读作业
    java课程设计--WeTalk(201521123072秦贞一)
    201521123072《java程序设计》第十四周学习总结
  • 原文地址:https://www.cnblogs.com/wf123/p/9935450.html
Copyright © 2020-2023  润新知