1、目标

举例说明在vue生命周期里el属性、data属性,computed属性,
watch属性,methods属性的变化情况
复制代码

html代码如下

<div id="app"><p @click="click">{{ message }}</p><p>{{ computedMessage }}</p>
</div>
复制代码

js代码如下(大致看一下就行了,最后看一下控制台打印结果就一目了然)

function destroy(){app.$destroy()
}
var app = new Vue({el: '#app',data: {message: "hello"},methods:{click: function(){destroy()console.log('click')}},watch: {message: function(){console.log('watcher')}},computed: {computedMessage: function(){return this.message + ' computed'}},beforeCreate: function () {console.group('beforeCreate 创建前状态===============》');console.log("%c%s", "color:red", "el     : " +JSON.stringify(this.$el)); console.log("%c%s", "color:red", "data   : " + this.$data);  console.log("%c%s", "color:red", "message: " + this.message)console.log("%c%s", "color:red", "computed: " + this.computedMessage)console.log("%c%s", "color:red", "methods: " + this.click)console.log("%c%s", "color:red", "_watchers: " + this._watchers)},created: function () {console.group('created 创建完毕状态===============》');console.log("%c%s", "color:red", "el     : " + JSON.stringify(this.$el)); console.log("%c%s", "color:red", "data   : " + this.$data);  console.log("%c%s", "color:red", "message: " + this.message); console.log("%c%s", "color:red", "computed: " + this.computedMessage)console.log("%c%s", "color:red", "methods: " + this.click)console.log("%c%s", "color:red", "_watchers: " + this._watchers)},beforeMount: function () {console.group('beforeMount 挂载前状态===============》');console.log("%c%s", "color:red", "el     : " + JSON.stringify(this.$el)); console.log("%c%s", "color:red", "data   : " + this.$data);   console.log("%c%s", "color:red", "message: " + this.message); console.log("%c%s", "color:red", "computed: " + this.computedMessage)console.log("%c%s", "color:red", "methods: " + this.click)console.log("%c%s", "color:red", "_watchers: " + this._watchers) },mounted: function () {console.group('mounted 挂载结束状态===============》');console.log("%c%s", "color:red", "el     : " + this.$el); console.log("%c%s", "color:red", "data   : " + this.$data); console.log("%c%s", "color:red", "message: " + this.message); console.log("%c%s", "color:red", "computed: " + this.computedMessage)console.log("%c%s", "color:red", "methods: " + this.click)console.log("%c%s", "color:red", "_watchers: " + this._watchers) },beforeUpdate: function () {console.group('beforeUpdate 更新前状态===============》');console.log("%c%s", "color:red", "el     : " +this.$el);console.log("%c%s", "color:red", "data   : " + this.$data);console.log("%c%s", "color:red", "message: " + this.message);console.log("%c%s", "color:red", "computed: " + this.computedMessage)console.log("%c%s", "color:red", "methods: " + this.click)console.log("%c%s", "color:red", "_watchers: " + this._watchers)},updated: function () {console.group('updated 更新完成状态===============》');console.log("%c%s", "color:red", "el     : " +this.$el);console.log("%c%s", "color:red", "data   : " + this.$data);console.log("%c%s", "color:red", "message: " + this.message);console.log("%c%s", "color:red", "computed: " + this.computedMessage)console.log("%c%s", "color:red", "methods: " + this.click)console.log("%c%s", "color:red", "_watchers: " + this._watchers)},beforeDestroy: function () {console.group('beforeDestroy 销毁前状态===============》');console.log("%c%s", "color:red", "el     : " +this.$el);console.log("%c%s", "color:red", "data   : " + this.$data);console.log("%c%s", "color:red", "message: " + this.message);console.log("%c%s", "color:red", "computed: " + this.computedMessage)console.log("%c%s", "color:red", "methods: " + this.click)console.log("%c%s", "color:red", "_watchers: " + this._watchers)},destroyed: function () {console.group('destroyed 销毁完成状态===============》');console.log("%c%s", "color:red", "el     : " +this.$el);console.log("%c%s", "color:red", "data   : " + this.$data);console.log("%c%s", "color:red", "message: " + this.message)console.log("%c%s", "color:red", "computed: " + this.computedMessage)console.log("%c%s", "color:red", "methods: " + this.click)console.log("%c%s", "color:red", "_watchers: " + this._watchers)}})
复制代码

2、数据初始化阶段,生命周期变化

初始化触发四个阶段: beforeCreate、created、beforeMount、mounted
复制代码
1、可以看到,beforeCreate阶段,什么都没有初始化
2、created阶段
2.1 el没有初始化
2.2 data有数据了
2.3 computed有数据了
2.4 methods 有数据了
2.5 watcher 有监控了
复制代码
3、beforeMount阶段
3.1 el初始化了,但没有挂载
3.2 data有数据了
3.3 computed有数据了
3.4 methods 有数据了
3.5 watcher 有监控了4、mounted阶段
4.1 el初始化了,也挂在到dom上了
4.2 data有数据了
4.3 computed有数据了
4.4 methods 有数据了
4.5 watcher 有监控了,注意,这里的watcher比上一个阶段多了一个
注:
第一个watcher是监控message属性变化,就刷新视图的
第二个watcher是监控computed里computedMessage属性的变化
但是computedMessage属性不能setter只能getter
多出来的一个watcher是关注vm._update属性的变化,然后刷新视图
复制代码

3、数据更新阶段,生命周期变化

我们调用app.message = 'heihei';观察生命周期的变化(跟上面mounted阶段一样)
复制代码

4、销毁组建

调用app.destory();watcher全部卸载,也就是调用app.message=123,是无效的
虽然下图看起来watcher还在,但是watcher有个active属性,是false了,
也就是说所有属性,事件都在,只是不能响应式变化了
复制代码

急速了解vue生命周期相关推荐

  1. Vue 生命周期记录_学习笔记

    官方给出的设计图入戏 为了能更好的理解这个图呢,写了下面的demo <!DOCTYPE html> <html><head><meta charset=&qu ...

  2. 标注图+部分举例聊聊Vue生命周期

    "你不需要立马弄明白所有的东西,不过随着你的不断学习和使用,它的参考价值会越来越高." 现在项目中遇到了,好好回头总结一波Vue生命周期,以后用到的时候再来翻翻. 啥叫Vue生命周 ...

  3. vue hot true 不起作用_从源码解读 Vuex 注入 Vue 生命周期的过程

    第一篇文章我会结合 Vue 和 Vuex 的部分源码,来说明 Vuex 注入 Vue 生命周期的过程. 说到源码,其实没有想象的那么难.也和我们平时写业务代码差不多,都是方法的调用.但是源码的调用树会 ...

  4. 少侠请重新来过 - Vue学习笔记(二) - Vue生命周期

    Vue 生命周期和钩子 每一个vue实例创建时,会经历一段初始化的过程,同时会调用其生命周期钩子,实例的钩子this指向它的Vue实例,不需要在钩子用箭头函数. <template>< ...

  5. vue生命周期图示中英文版Vue实例生命周期钩子

    vue生命周期图示中英文版Vue实例生命周期钩子 知乎上近日有人发起了一个 "react 是不是比 vue 牛皮,为什么?" 的问题,Vue.js 作者尤雨溪12月4日正面回应了该 ...

  6. 异步加载在Vue生命周期哪个阶段更合理

    react高阶面试题中有这么一道:为什么异步请求数据在didMount阶段更合适?同为MVVM中的翘楚,Vue是否也有类似问题呢?另外,我在平时也无开发过程中也会发现,每个人选择的那个生命周期阶段去异 ...

  7. 一文带你吃透Vue生命周期(结合案例通俗易懂)

    文章目录 本篇学习目标 1. vue生命周期 1.0_人的-生命周期 1.1_钩子函数 1.2_初始化阶段 1.3_挂载阶段 1.4_更新阶段 1.5_销毁阶段 2. axios 2.0_axios基 ...

  8. 详解vue生命周期及每个阶段适合进行的操作

    VUE生命周期的四个阶段 create 创建 -------- 创建vue实例并初始化 mount 挂载 -------- 把vue实例和视图进行关联 update 更新 ------- 监听数据与视 ...

  9. Vue 生命周期LIFECYCLE是8个吗?

    vue生命周期钩子个数是:11个. export const LIFECYCLE_HOOKS = [ 'beforeCreate', 'created', 'beforeMount', 'mounte ...

最新文章

  1. 通过WMIC命令远程打开远程计算机的远程桌面(Remote Desktop)功能
  2. Scala操作外部数据
  3. Python: 反方向迭代一个序列
  4. Linux 下定时文件crontab配置
  5. 【LeetCode】剑指 Offer 14. 剪绳子
  6. 三星开出的57619美元年薪 却还是留不住千禧一代
  7. 阿里布局无人驾驶;滴滴成立汽车服务;“京东 AI 天团”首亮相| CSDN极客头条...
  8. python小程序100题-Python 练习册,每天一个小程序 -- 0000题
  9. bootstrap 检验 法 原理_Bootstrap教程-用SPSS中的Process插件做中介效应分析
  10. python创建ppt_python生成ppt的方法
  11. c语言整形数组存放字符串,用一维字符数组存放字符串
  12. 陕西省本级城镇企业退休人员 - 人脸识别APP资格认证操作指南
  13. 2022年郑州市初级焊工考试模拟试题及答案
  14. 南京理工大学计算机学院施静,“为是这个学院的一分子而骄傲”----院友风采(2014届):南京理工大学最年轻教授 祁志祥...
  15. 解决Module build failed: Error: Cannot find module ‘node-sass‘Require stack
  16. React 待办事项列表案例
  17. python四级考试_四级英语考试如何准备呢?
  18. 卡巴斯基 取消 远程控制 限制
  19. Goreleaser + TraivsCI 发布 gopo 项目
  20. (轉貼) Ubuntu 7.10 Linux on ThinkPad X61 安裝筆記 (NB) (ThinkPad) (X61) (Linux) (Ubuntu)

热门文章

  1. 第五篇:你“ 看不见 ” 的隐式转换
  2. SQL Server 2012 - 数据表的操作
  3. Python中的open和codecs.open
  4. DatePicker 和 DatePickerDialog的基本使用方法
  5. Java开发人员可以从Spring框架中学到编程技巧
  6. zip压缩/tar打包
  7. Tomas语录-关于String.valueOf()
  8. Ubuntu Git安装与使用
  9. for-in和for-of,forEach和Map
  10. 控制kvm-qcow2增长空间-(一)