Vue——生命周期

目录

一、 生命周期图

二、再探究

2.1 beforeCreate之前

2.2 beforeCreate和created钩子函数间的生命周期

2.3 created钩子函数和beforeMount间的生命周期

2.3.1 el选项对生命周期影响

2.3.2 template

2.4 beforeMount和mounted钩子函数间的生命周期

2.5 beforeUpdate钩子函数和updated钩子函数间的生命周期

2.6 beforeDestroy和destroyed钩子函数间的生命周期

2.6.1 beforeDestroy

2.6.2 destroyed

三、总结

总线机制 发布订阅者模式

自定义指令(全局和局部) 全局

局部


一、 生命周期图

​ Vue实例的生命周期中有多个状态。

测试代码

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	
</head>
<body>
	<div id="root">
    
	</div>
</body>
<script type="text/javascript" src="./vue.js"></script>
<script type="text/javascript">
  var vm = new Vue({
  	el : '#root',
	data(){
		return{
			msg:"111"
		}
		
	},
	template:'<div>{
  {msg}}</div>',
	beforeCreate:function(){
		console.log('beforeCreate');
	}, 
	created:function(){
		console.log('created');
	}, 
	beforeMount:function(){
		console.log(this.$el);
		console.log('beforeMount');
	},
	mounted:function(){
		console.log(this.$el);
		console.log('Mount');
	},
	
	beforeUpdate(){
		console.log("调用了beforeUpdate钩子函数");
	},
	updated(){
		console.log("调用了Update钩子函数");
	},
	beforeDestroy(){
		      console.log("调用了beforeDestroy钩子函数");
		    },
	destroyed(){
		      console.log("调用了destroyed钩子函数");
		    },
  })

</script>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Vue实例的生命周期</title>
</head>
<body>
<div id="app">
  <h1>测试生命周期</h1>
  <div>{
  {msg}}</div>
  <hr>
  <h3>测试beforeUpdate和update两个钩子函数</h3>
  <button @click="handlerUpdate">更新数据</button>
</div>
<script src="../js/vue.js"></script>
<script>
  const app = new Vue({
    el: '#app',
    data: {
      msg: "12345"
    },
    methods: {
      handlerUpdate() {
        this.msg=this.msg.split("").reverse().join("")
      }
    },
    //按照示意图依次调用
    beforeCreate(){
      console.log("调用了beforeCreate钩子函数");
    },
    created(){
      console.log("调用了created钩子函数");
    },
    beforeMount(){
      console.log('调用了beforeMount钩子函数');
    },
    mounted(){
      console.log('调用了mounted钩子函数');
    },
    beforeUpdate(){
      console.log("调用了beforeUpdate钩子函数")
    },
    updated(){
      console.log("调用了updated钩子函数");
    },
    beforeDestroy(){
      console.log("调用了beforeDestroy钩子函数");
    },
    destroyed(){
      console.log("调用了destroyed钩子函数");
    }
  })
</script>
</body>
</html>

如图所示:

初始化页面依次调用了:

  1. 调用了beforeCreate钩子函数
  2. 调用了created钩子函数
  3. 调用了beforeMount钩子函数
  4. 调用了mounted钩子函数

点击更新数据后:

12345变成了54321,此时调用了:

  1. 调用了beforeUpdate钩子函数
  2. 调用了updated钩子函数

打开F12控制台 直接输入app.$destroy()主动销毁Vue实例调用:

  1. 调用了beforeDestroy钩子函数
  2. 调用了destroyed钩子函数

二、再探究

2.1 beforeCreate之前

初始化钩子函数和生命周期

2.2 beforeCreate和created钩子函数间的生命周期

在beforeCreate和created之间,进行数据观测(data observer) ,也就是在这个时候开始监控data中的数据变化了,同时初始化事件。

2.3 created钩子函数和beforeMount间的生命周期

对于created钩子函数和beforeMount有判断: 

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值