生命周期和钩子函数在Vue的学习的里是很重要的,这篇文章里我浅显地说说钩子函数( ̄︶ ̄)
Vue的生命周期图

beforeCreate、created、beforeMount、mounted、beforeUpdate、updated、beforeDestroy、destroyed八个钩子函数,下面用代码简单测试一下。简单代码测试
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script src="vue.js"></script>
</head>
<body>
<div id="a">
<p>{{ num }}</p>
<button @click="updateNum">update</button>
</div>
<button id="destroy">destroy</button>
<script>
var vm = new Vue({
el : '#a',
data : {
num : 0
},
methods : {
updateNum : function(){
this.num ++;
}
},
beforeCreate : function(){
console.log("beforeCreate的num:",this.num);
console.log("beforeCreate的$el:",this.$el);
},
created : function(){
console.log("created的num:",this.num);
console.log("created的$el:",this.$el);
},
beforeMount : function(){
console.log("beforeMount的$el:",this.$el);
},
mounted : function(){
console.log("mounted的$el:",this.$el);
},
beforeUpdate : function(){
console.log("beforeUpdate的$el:",this.$el.querySelector('p').innerText);
},
updated : function(){
console.log("updated的$el:",this.$el.querySelector('p').innerText);
},
})
document.getElementById('destroy').addEventListener('click',function(){
vm.$destroy();
})
</script>
</body>
</html>
1.beforeCreate
在实例初始化之后,在数据监听和事件配置之前触发。
从开发者工具里得到console.log()的输出内容:

beforeCreate这个阶段,属性是还没被看到的,DOM也还没有生成。
2.created

3.beforeMount

4.mounted

5. beforeUpdate

6.updated

7.beforeDestroy和destroyed
当我们告知要用vm.$destroy()移除掉这个组件了,就会触发beforeDestroy,接着它就会开始移除,移除完毕就是destroyed。

总结
把上面的钩子函数的内容总结一下,做表如下:

有什么建议或者我有什么写得不好的,望告知,蟹蟹~🦀🦀