欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Vue.js之生命周期

程序员文章站 2022-04-24 15:57:52
有时候,我们需要在实例创建过程中进行一些初始化的工作,以帮助我们完成项目中更复杂更丰富的需求开发,针对这样的需求,Vue提供给我们一系列的钩子函数。 vue生命周期 beforeCreate 在实例初始化之后,数据观测 (data observer) 和 event/watcher 事件配置之前被调 ......

有时候,我们需要在实例创建过程中进行一些初始化的工作,以帮助我们完成项目中更复杂更丰富的需求开发,针对这样的需求,vue提供给我们一系列的钩子函数。

vue生命周期

Vue.js之生命周期

beforecreate

在实例初始化之后,数据观测 (data observer) 和 event/watcher 事件配置之前被调用。

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>title</title>
    <script src="../statics/vue.js"></script>
</head>
<body>

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

    <script>

        new vue({
            el: "#app",
            // 在template中使用组件与在body中使用组件是一样的
            // template: `<cont></cont>`,
            data: {
                name: "alex"
            },
            methods: {
                init: function () {
                    console.log(this.name);
                },
                myclick: function () {
                    this.name = "pizza";
                }
            },
            beforecreate() {
                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>

</body>
</html>

效果:

Vue.js之生命周期

created

在实例创建完成后被立即调用。在这一步,实例已完成以下的配置:数据观测 (data observer),属性和方法的运算,watch/event 事件回调。然而,挂载阶段还没开始,$el 属性目前不可见。

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>title</title>
    <script src="static/vue.min.js"></script>
</head>
<body>

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

    <script>

        new vue({
            el: "#app",
            // 在template中使用组件与在body中使用组件是一样的
            // template: `<cont></cont>`,
            data: {
                name: "alex"
            },
            methods: {
                init: function () {
                    console.log(this.name);
                },
                myclick: function () {
                    this.name = "pizza";
                }
            },
            beforecreate() {
                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);
            },
            created() {
                console.group("created");
                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>

</body>
</html>

beforemount

在挂载开始之前被调用:相关的 render 函数首次被调用。

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>title</title>
    <script src="../statics/vue.js"></script>
</head>
<body>

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

    <script>

        new vue({
            el: "#app",
            // 在template中使用组件与在body中使用组件是一样的
            // template: `<cont></cont>`,
            data: {
                name: "alex"
            },
            methods: {
                init: function () {
                    console.log(this.name);
                },
                myclick: function () {
                    this.name = "pizza";
                }
            },
            beforecreate() {
                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);
            },
            created() {
                console.group("created");
                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);
            },
            beforemount() {
                console.group("beforemount");
                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>

</body>
</html>

mounted

el 被新创建的 vm.$el 替换,并挂载到实例上去之后调用该钩子。如果 root 实例挂载了一个文档内元素,当 mounted 被调用时 vm.$el 也在文档内。

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>title</title>
    <script src="../statics/vue.js"></script>
</head>
<body>

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

    <script>

        new vue({
            el: "#app",
            // 在template中使用组件与在body中使用组件是一样的
            // template: `<cont></cont>`,
            data: {
                name: "alex"
            },
            methods: {
                init: function () {
                    console.log(this.name);
                },
                myclick: function () {
                    this.name = "pizza";
                }
            },
            beforecreate() {
                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);
            },
            created() {
                console.group("created");
                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);
            },
            beforemount() {
                console.group("beforemount");
                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);
            },
            mounted() {
                console.group("mounted");
                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>

</body>
</html>

beforeupdate

数据更新时调用,发生在虚拟 dom 打补丁之前。这里适合在更新之前访问现有的 dom,比如手动移除已添加的事件监听器。

该钩子在服务器端渲染期间不被调用,因为只有初次渲染会在服务端进行.

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>title</title>
    <script src="../statics/vue.js"></script>
</head>
<body>

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

    <script>

        new vue({
            el: "#app",
            // 在template中使用组件与在body中使用组件是一样的
            // template: `<cont></cont>`,
            data: {
                name: "alex"
            },
            methods: {
                init: function () {
                    console.log(this.name);
                },
                myclick: function () {
                    this.name = "pizza";
                }
            },
            beforecreate() {
                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);
            },
            created() {
                console.group("created");
                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);
            },
            beforemount() {
                console.group("beforemount");
                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);
            },
            mounted() {
                console.group("mounted");
                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);
            },
            beforeupdate() {
                console.group("beforeupdate");
                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>

</body>
</html>

updated

由于数据更改导致的虚拟 dom 重新渲染和打补丁,在这之后会调用该钩子。

当这个钩子被调用时,组件 dom 已经更新,所以你现在可以执行依赖于 dom 的操作。然而在大多数情况下,你应该避免在此期间更改状态。如果要相应状态改变,通常最好使用或  取而代之。

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>title</title>
    <script src="../statics/vue.js"></script>
</head>
<body>

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

    <script>

        new vue({
            el: "#app",
            // 在template中使用组件与在body中使用组件是一样的
            // template: `<cont></cont>`,
            data: {
                name: "alex"
            },
            methods: {
                init: function () {
                    console.log(this.name);
                },
                myclick: function () {
                    this.name = "pizza";
                }
            },
            beforecreate() {
                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);
            },
            created() {
                console.group("created");
                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);
            },
            beforemount() {
                console.group("beforemount");
                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);
            },
            mounted() {
                console.group("mounted");
                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);
            },
            beforeupdate() {
                console.group("beforeupdate");
                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);
            },
            updated() {
                console.log("updated");
                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>

</body>
</html>

activated

keep-alive 组件激活时调用。<keep-alive> 包裹动态组件时,会缓存不活动的组件实例,而不是销毁它们

该钩子在服务器端渲染期间不被调用。

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>title</title>
    <script src="../statics/vue.min.js"></script>
</head>
<body>

    <div id="app">
        <app></app>
    </div>

    <script>
        let laside = {
            template: `
                <div>
                    <h1>{{ message }}</h1>
                    <button @click="changedata">点击修改数据</button>
                </div>
            `,

            data () {
                return {
                    message: "hello vue!"
                }
            },

            methods: {
                init: function () {
                    console.log(this.message)
                }
                changedata: function () {
                    this.mes = "pizza is here!";
                }
            },

            // 组件的创建和销毁对性能有影响
            beforedestroy () {
                console.log("beforedestroy");
            },

            destroyed () {
                console.log("destroyed");
            },

            activated () {
                console.group("activated");
                console.log("el: ", this.$el);
                console.log("data: ", this.$data);
                console.log("message: ", this.message);
                console.log("init: ", this.init);
                console.log("innerhtml: ", document.getelementbyid("app").innerhtml);
            },
        };

        let app = {
            template: `
                <div >
                    <keep-alive>
                        <laside v-if="isshow"></laside>
                    </keep-alive>
                    <button @click="showhide">创建消除组件</button>
                </div>
            `,
            components: {
                "laside": laside,
            },
            methods: {
                showhide: function () {
                    this.isshow = !this.isshow;
                }
            },
            data () {
                return {
                    isshow: true,
                }
            }
        };

        new vue({
            el: "#app",
            // 在template中使用组件与在body中使用组件是一样的
            // template: `<cont></cont>`,
            components: {
                app,
            }
        })
    </script>

</body>
</html>

deactivated

keep-alive 组件停用时调用。

该钩子在服务器端渲染期间不被调用。

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>title</title>
    <script src="../statics/vue.min.js"></script>
</head>
<body>

    <div id="app">
        <app></app>
    </div>

    <script>
        let laside = {
            template: `
                <div>
                    <h1>{{ mes }}</h1>
                    <button @click="changedata">点击修改数据</button>
                </div>
            `,

            data () {
                return {
                    message: "hello vue!"
                }
            },

            methods: {
                init: function () {
                    console.log(this.message);
                }
                changedata: function () {
                    this.mes = "pizza is here!";
                }
            },

            // 组件的创建和销毁对性能有影响
            beforedestroy () {
                console.log("beforedestroy");
            },

            destroyed () {
                console.log("destroyed");
            },

            activated () {
                console.group("activated");
                console.log("el: ", this.$el);
                console.log("data: ", this.$data);
                console.log("message: ", this.message);
                console.log("init: ", this.init);
                console.log("innerhtml: ", document.getelementbyid("app").innerhtml);
            },

            deactivated () {
                console.group("deactivated");
                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);
            }
        };

        let app = {
            template: `
                <div >
                    <keep-alive>
                        <laside v-if="isshow"></laside>
                    </keep-alive>
                    <button @click="showhide">创建消除组件</button>
                </div>
            `,
            components: {
                "laside": laside,
            },
            methods: {
                showhide: function () {
                    this.isshow = !this.isshow;
                }
            },
            data () {
                return {
                    isshow: true,
                }
            }
        };

        new vue({
            el: "#app",
            // 在template中使用组件与在body中使用组件是一样的
            // template: `<cont></cont>`,
            components: {
                app,
            }
        })
    </script>

</body>
</html>

beforedestroy

实例销毁之前调用。在这一步,实例仍然完全可用。频繁的创建和销毁组件对性能的影响很大,因此可以使用activated和deactivated。

该钩子在服务器端渲染期间不被调用。

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>title</title>
    <script src="../statics/vue.js"></script>
</head>
<body>

    <div id="app">
        <app></app>
    </div>

    <script>
        let laside = {
            template: `
                <div>
                    <h1>{{ mes }}</h1>
                    <button @click="changedata">点击修改数据</button>
                </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.name);
                console.log("init: ", this.init);
                console.log("innertext: ", document.getelementbyid("app").innertext);
                console.log("innerhtml: ", document.getelementbyid("app").innerhtml);
            },
        };

        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中使用组件与在body中使用组件是一样的
            // template: `<cont></cont>`,
            components: {
                app,
            }
        })
    </script>

</body>
</html>

destroyed

vue 实例销毁后调用。调用后,vue 实例指示的所有东西都会解绑定,所有的事件监听器会被移除,所有的子实例也会被销毁。

该钩子在服务器端渲染期间不被调用。

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>title</title>
    <script src="../statics/vue.min.js"></script>
</head>
<body>

    <div id="app">
        <app></app>
    </div>

    <script>
        let laside = {
            template: `
                <div>
                    <h1>{{ mes }}</h1>
                    <button @click="changedata">点击修改数据</button>
                </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.name);
                console.log("init: ", this.init);
                console.log("innerhtml: ", document.getelementbyid("app").innerhtml);
            },

            destroyed () {
                console.log("destroyed");
                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);
            }
        };

        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中使用组件与在body中使用组件是一样的
            // template: `<cont></cont>`,
            components: {
                app,
            }
        })
    </script>

</body>
</html>