Vue的生命周期

生命周期 => 重出生到死亡的一个过程
    Vue也自己的生命周期
        初始化阶段:执行一次
      创建阶段
          beforeCreate:创建之前
          created:创建完成            偶尔使用        有一些程序员在这里发送请求
      挂载阶段
          beforeMount:挂载之前
          mounted:挂载完成            经常使用        都在这里面发送请求
  执行多次
      更新阶段
          beforeUpdate:更新之前
          updated:更新完成
  销毁阶段
      beforeDestroy:销毁之前      使用一般            在组件死亡之前,我们一般处理的事情,关闭定时器
      destroyed:销毁完毕
KeepAlive中
激活
        activated() {
           1.在首次挂载、
            2.以及每次从缓存中被重新插入的时候调用
            },
            失活
            deactivated() {
                1.在从 DOM 上移除、进入缓存
                2.以及组件卸载时调用
            }

            拓展: 激活钩子里面的定时器,需要在失活里面进行清除
       在Vue实例里面 $ 是给程序员使用的
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
new Vue({
        /* template: 模板 */
        // template:`<h1>你好!</h1>`,
        template: "#box",
 
        data() {
          return {
            msg: "hello world",
            count: 0,
          };
        },
        /* 创建组件之前 */
        beforeCreate() {
          /* 读取到this,但是读取不到data */
          console.log("beforeCreate", this, this.msg);
        },
        /* 创建组件完成 */
        created() {
          /* 读取this,可以读取到data */
          console.log("created", this, this.msg);
        },
        /* 挂载组件之前 */
        beforeMount() {
          //可以读取到Vue解析之前的DOM
          console.log(
            "beforeMount",
            this,
            this.msg,
            document.getElementById("app").innerHTML
          );
        },
        /* 挂载组件完成 */
        mounted() {
          //可以读取到Vue解析之后的DOM
          console.log(
            "mounted",
            this,
            this.msg,
            document.getElementById("app").innerHTML
          );
        },
        /* 组件更新之前 */
        beforeUpdate() {
          //不要在这里更新数据
          //可以读取到更新之前的数据
          console.log(
            "beforeUpdate",
            this,
            this.msg,
            document.getElementById("app").innerHTML
          );
        },
        /* 组件更新之前 */
        updated() {
          //不要在这里更新数据
          // this.count ++
 
          //可以读取到更新之后的数据
          console.log(
            "updated",
            this,
            this.msg,
            document.getElementById("app").innerHTML
          );
        },
        //销毁之前
        beforeDestroy() {
          console.log("beforeDestroy");
        },
        destroyed() {
          console.log("destroyed");
        },
      }).$mount("#app");
    </script>

  

posted @   LT先生  阅读(23)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
点击右上角即可分享
微信分享提示