Vue.生命周期

生命周期图
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>生命周期</title>
</head>
<body>
    <style>
        #app{
            height: 1000px;
        }
    </style>
    <script src="../vue.js"></script>

    <div id="app">
        <button @click="isShow=false">销毁</button>
        <child v-if="isShow"></child>
    </div>

    <script>
        // 子组件
        Vue.component("child",{
            template:`
                <div>
                <button @click="title=11111">click</button>
                    child--{{title}}-{{count}}
                </div>
            `,
            // 状态
            data() {
                return {
                    title: "0000",
                    count: 0,
                    time: true
                }
            },
            // 方法
            methods: {
                
            },
            // 创建之前
            beforeCreate() {
                console.log("beforeCreate创建之前")
            },
            // 创建完成
            created() {
                console.log("created创建完成")
            },
            // 挂载之前
            beforeMount() {
                // 准备初始化模板引擎,准备渲染代码.
                console.log("beforeMount挂载之前")
            },
            //挂载完毕
            mounted() {
                // 初始化模板引擎,渲染代码,完毕.
                // 访问DOM,setInterval,window,onscroll,监听事件,ajax...
                console.log("mounted挂载完毕")
                
                // 定时器一直会触发 继续组件被销毁它还会触发
                this.time = setInterval(()=>{
                    console.log("setInterval")
                    this.count++
                },1000)

            },
            // 更新之前
            beforeUpdate() {
                console.log("beforeUpdate更新之前")
            },
            // 更新完成
            updated() {
                console.log("updated更新完成")
            },
            // 销毁之前
            beforeDestroy () {
                console.log("beforeDestroy销毁之前")
            },
            // 销毁完成
            destroyed() {
                console.log("destroyed销毁完成")
                // 在这要取消掉一些监听    
                
            },
        })

        new Vue({
            el: "#app",
            data:{ isShow:true },
            methods:{
                test() {
                    console.log(1)
                }
            },
            mounted() {
                window.addEventListener("scroll", ()=>{
                    console.log(1)
                })

            }
        })
    </script>
</body>
</html>
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容