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

    1,初始化显示:

    ①  beforeCreate()

    ② created()

    ③ beforeMount()

    ④ mounted()

    2,更新状态:this.xxx = value

    ① beforeUpdate()

    ② updated()

    3,销毁 Vue 实例:vm.$destory()

    ① beforeDestory()

    ② destoryed()

    4,常用的生命周期方法

    1)created() / mounted(),发送 ajax 请求,启动定时器等异步任务

    2)beforeDestory():收尾工作,如 清除定时器等。

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
    </head>
    <body>
        <div id="app">
            <button @click="destoryVue">destory vue实例</button>
            <p v-show="isShow">{{msg}}</p>
        </div>
        
        <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
        <script>
            var vm=new Vue({
                el:'#app',
                data:{
                    msg:"Hello,World.",
                    isShow:true
                },
                beforeCreate(){
                    console.log("beforeCreate,msg:",this.msg)
                },
                created(){
                    console.log("Created,msg:", this.msg);
                    this.intervalId=setInterval(()=>{
                        console.log("*********");
                        this.isShow=!this.isShow;
                    },1000)
                },
                updated(){
                    console.log("updated,msg:", this.msg);
                },
                beforeDestroy(){
                    console.log('beforeDestroy,msg:',this.msg)
                    clearInterval(this.intervalId);
                },
                destroyed(){
                      console.log('destroyed,msg:', this.msg)
                },
                methods:{
                    destoryVue(){
                        vm.$destroy();
                    }
                }
            })
        </script>
    </body>
    </html>
    

     打印结果:

  • 相关阅读:
    linux 卸载软件
    RYU基础整理
    FlowVisor相关
    RYU的GUI安装
    Ubuntu下apt-get与pip安装命令的区别
    mininet安装,使用
    Java中的Enum(枚举)用法介绍
    jquery选择器大全参考
    Oracle数据库SQL语句操作大全汇总
    SQLServer数据库语句大全汇总
  • 原文地址:https://www.cnblogs.com/shanlu0000/p/13456128.html
Copyright © 2011-2022 走看看