vue生命周期

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title></title>
    <style>
        .test {
            width: 100px;
            height: 100px;
            background-color: skyblue;
        }
    </style>
</head>

<body>
    <div id="app">
        <button @click='change'>点击更新</button>
        <button @click='destory'>点击销毁</button>
        <div class="test">
            {{msg}}
        </div>
    </div>
 <!-- 生命周期可看做一个一个的时间节点,在到达对应的时间节点以后执行相应的函数 -->
    <script src="js/vue.js"></script>
    <script>
        var vm = new Vue({
            //此时vue组件实例刚刚创建,但是还没有创建完,不能访问其属性
            beforeCreate() {
                console.log(' beforeCreated', this.msg);
            },
            //此时vue组件实例已经创建好了,但是还没生成模板,但是已经可以访问data,methods属性等
            created() {
                console.log('created', this.msg);
            },
            //此时组件实例刚刚创建好,同时模板结构也在内存中生成
            //虚拟dom已经生成在内存中,但是还没有渲染到页面上,可以获取data等成员,但是不能访问页面元素
            beforeMount() {
                console.log('beforeMount', this.msg);
            },
            //此时组件已经全部创建完毕,模板也渲染到页面上
            mounted() {
                console.log('mounted', this.msg);
            },
            //此时数据更新了,但是还没渲染到页面上,也就是说模板也更新了,但是还没同步到页面上
            beforeUpdate() {
                console.log('beforeupdate', this.msg);
            },
         //组件销毁之前
            updated() {
                console.log('updated', this.msg);
            },
            //组件完全销毁
            beforeDestroy() {
                console.log('beforedestory', this.msg);
            },
            destroyed() {
                console.log('destory', this.msg);
            },
            el: "#app",
            data: {
                msg: '你看我在不在'
            },
            methods: {
                change() {
                    this.msg = '你看我更没更新'
                },
                destory() {
                    this.msg = '你看我销没销毁'
                    this.$destroy()
                }
            },
        })
    </script>
</body>

</html>

 

 

posted @ 2020-08-07 20:58  诗和远方哟  阅读(99)  评论(0)    收藏  举报