0
点赞
收藏
分享

微信扫一扫

VUE四个生命阶段和8个钩子函数的执行及其实现效果------VUE框架

VUE四个生命阶段和8个钩子函数的执行及其实现效果

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="../js/vue.js"></script>
</head>
<body>
    <div id="app">
        <h1>{{msg}}:{{count}}</h1>
        <button @click="add()">按一下加一</button>
        <button @click="destroy()">按下销毁VM</button>
    </div>
    <script>
        // 初始阶段,挂载阶段,更新阶段,销毁阶段
        // 一共是四个阶段,八个钩子函数
        // 初始阶段beforeCreate创建前,created创建后
        // 挂载阶段beforeMount挂载前,mounted挂载后
        // 更新阶段beforeUpdate更新前,updated更新后
        // 销毁阶段beforeDestroy销毁前,destroyed销毁后
        const vm = new Vue({
            el : "#app",
            data : {
                msg : "Hello",
                count : 1
            },
            methods : {
                add()
                {
                    this.count++;
                },
                destroy()
                {
                    this.$destroy();
                }
            },
            // 初始阶段
            beforeCreate(){
                console.log("VUE对象初始化了");
            },
            created(){
                console.log("created");
            },
            // 挂载阶段
            beforeMount(){
                console.log("VUE对象挂载了");
            },
            mounted(){
                console.log("mounted");
            },
            // 更新阶段
            beforeUpdate(){
                console.log("VUE对象的data更新了");
            },
            updated()
            {
                console.log("updated");
            },
            // 销毁阶段
            beforeDestroy(){
                console.log("VM对象销毁了");
            },
            destroyed()
            {
                console.log("destroyed");
            }
        });
    </script>
</body>
</html>

举报

相关推荐

0 条评论