zoukankan      html  css  js  c++  java
  • Vue之生命周期

    Vue之生命周期

    1.beforeCreate

      el: 没有

      data: 没有
           事件: 没有被初始化
           innerHTMl:
               <div>
                   {{ name }}
                    <button @click="myClick">点击事件</button>
               </div>

    <!DOCTYPE html>
    <html lang="zh-CN">
    <head>
        <meta http-equiv="content-Type" charset="UTF-8">
        <meta http-equiv="x-ua-compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <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",
                data: {
                    name: "maweihua",
                },
                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>
    

    2.created

      el: 没有
           data: 数据有了
           事件:事件有了
           innerHTMl:
           <div>
                {{ name }}
                <button @click="myClick">点击事件</button>
           </div>

    <!DOCTYPE html>
    <html lang="zh-CN">
    <head>
        <meta http-equiv="content-Type" charset="UTF-8">
        <meta http-equiv="x-ua-compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <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",
                data: {
                    name: "maweihua",
                },
                methods: {
                    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('myClick', this.myClick);
                    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('myClick', this.myClick);
                    console.log("innerHTML: ", document.getElementById("app").innerHTML);
                }
            })
        </script>
    
    </body>
    </html>
    

    3.beforeMount

      el: 找到了
           data: 数据有了
           事件:事件有了
           innerHTMl:
           <div>
               {{ name }}
               <button @click="myClick">点击事件</button>
           </div>

            beforeMount() {
                    console.group("beforeMount");
                    console.log("el: ", this.$el);
                    console.log("data: ", this.$data);
                    console.log("name: ", this.name);
                    console.log('myClick', this.myClick);
                    console.log("innerHTML: ", document.getElementById("app").innerHTML);
                    }    
    

    4.mounted

      el: 找到了,并且数据被渲染进标签了
           data: 数据有了,被监听
           事件:事件有了,被监听
           innerHTMl:
                 <div>
                     maweihua
                     <button @click="myClick">点击事件</button>
                </div>

            mounted() {
                    console.group("mounted");
                    console.log("el: ", this.$el);
                    console.log("data: ", this.$data);
                    console.log("name: ", this.name);
                    console.log('myClick', this.myClick);
                    console.log("innerHTML: ", document.getElementById("app").innerHTML);
                }
    

    5.beforeUpdate

      el: 找到了,并且数据被渲染进标签了
           data: 数据有了,被监听
           事件:事件有了,被监听
           innerHTMl:
                <div>
                    maweihua
                    <button @click="myClick">点击事件</button>
                </div>

            beforeUpdate() {
                    console.group("beforeUpdate");
                    console.log("el: ", this.$el);
                    console.log("data: ", this.$data);
                    console.log("name: ", this.name);
                    console.log('myClick', this.myClick);
                    console.log("innerHTML: ", document.getElementById("app").innerHTML);
                }
    

    6.updated

      el: 找到了,并且数据被渲染进标签了
           data: 数据有了,被监听
           事件:事件有了,被监听
           innerHTMl:
                <div>
                    maweihua
                    <button @click="myClick">点击事件</button>
                </div>

            updated() {
                    console.group("updated");
                    console.log("el: ", this.$el);
                    console.log("data: ", this.$data);
                    console.log("name: ", this.name);
                    console.log('myClick', this.myClick);
                    console.log("innerHTML: ", document.getElementById("app").innerHTML);
                }
    

    7.beforeDestroy

    <!DOCTYPE html>
    <html lang="zh-CN">
    <head>
        <meta http-equiv="content-Type" charset="UTF-8">
        <meta http-equiv="x-ua-compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>Title</title>
        <script src="../static/vue.min.js"></script>
    </head>
    <body>
    
        <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>
    
    </body>
    </html>
    

    8.destroyed

            destroyed() {
                    console.log("destroyed");
                    console.log("el: ", this.$el);
                    console.log("data: ", this.$data);
                    console.log("name: ", this.mes);
                },
    

    9.activated

    <!DOCTYPE html>
    <html lang="zh-CN">
    <head>
        <meta http-equiv="content-Type" charset="UTF-8">
        <meta http-equiv="x-ua-compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>Title</title>
        <script src="../static/vue.min.js"></script>
    </head>
    <body>
    
        <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);
                },
                destroyed() {
                    console.log("destroyed");
                    console.log("el: ", this.$el);
                    console.log("data: ", this.$data);
                    console.log("name: ", this.mes);
                },
                activated() {
                    console.group("activated");
                    console.log("el: ", this.$el);
                    console.log("data: ", this.$data);
                    console.log("name: ", this.mes);
                },
            };
    
            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: `<App/>`,
                components: {
                    App,
                }
            })
        </script>
    
    </body>
    </html>
    

    10.deactivated

            deactivated() {
                    console.group("deactivated");
                    console.log("el: ", this.$el);
                    console.log("data: ", this.$data);
                    console.log("name: ", this.mes);
                },
    

  • 相关阅读:
    ASP.NET中几种加密方法
    Linux Mint17.1安装PHPStorm8.0.2
    HTTP 错误 500.23
    Kali Linux 下安装配置MongoDB数据库 ubuntu 下安装配置MongoDB源码安装数据库
    如何在Ubuntu 18.04 LTS上安装和配置MongoDB
    scrapy 如何链接有密码的redis scrapy-redis 设置redis 密码 scrapy-redis如何为redis配置密码
    Redis报错:DENIED Redis is running in protected mode
    ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2 "No such file or directory")
    用mingw32编译ffmpeg2.7
    VS2005编译QT4.8.2
  • 原文地址:https://www.cnblogs.com/wjs521/p/9948293.html
Copyright © 2011-2022 走看看