VUE生命周期函数

每个 Vue 实例在被创建时都要经过一系列的初始化过程——例如,需要设置数据监听、编译模板、将实例挂载到 DOM 并在数据变化时更新 DOM 等。同时在这个过程中也会运行一些叫做生命周期钩子的函数,这给了用户在不同阶段添加自己的代码的机会。从开始创建、初始化数据、编译模板、挂载Dom、渲染→更新→渲染、卸载等一系列过程,参考官网图片
在这里插入图片描述

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Hello Vue</title>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>

</head>
<body>
	<div id="app">
		<span v-bind:style="{ color: activeColor, fontSize: fontSize + 'px' }">{{foo}}</span>
	</div>
<script>
var obj = {
	activeColor:'red',
	fontSize:'12',
	foo: '<font color="red">Hello Html Fragment</font>'
}

var vm = new Vue({
  el: '#app',
  data: obj,
  beforeCreate:function()
  {
	  console.log('beforeCreate...');
  },
  created:function()
  {
	  console.log('create...');
  },
  beforeMount:function()
  {
	  console.log('beforeMount...');
  },
  mounted:function()
  {
	  console.log('mounted...');
  },
  beforeUpdate:function()
  {
	  console.log('beforeUpdate...');
  },
  updated:function()
  {
	  console.log('updated...');
  },
  beforeDestroy:function()
  {
	  console.log('beforeDestroy...');
  },
  destroyed:function()
  {
	  console.log('destroyed...');
  }
})
</script>
</body>
</html>

在这里插入图片描述
在这里插入图片描述

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Hello Vue</title>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>

</head>
<body>
	<div id="app">
		<span v-bind:style="{ color: activeColor, fontSize: fontSize + 'px' }">{{foo}}</span>
	</div>
<script>
var obj = {
	activeColor:'red',
	fontSize:'12',
	foo: '<font color="red">Hello Html Fragment</font>'
}

var vm = new Vue({
  el: '#app',
  data: obj,
  beforeCreate:function()
  {
	  console.log('beforeCreate...');
  },
  created:function()
  {
	  console.log('create...');
	  //1秒后更新数据
	  setTimeout(()=>this.foo='Sun',1000);
  },
  beforeMount:function()
  {
	  console.log('beforeMount...');
  },
  mounted:function()
  {
	  console.log('mounted...');
  },
  beforeUpdate:function()
  {
	  console.log('beforeUpdate...');
  },
  updated:function()
  {
	  console.log('updated...');
  },
  beforeDestroy:function()
  {
	  console.log('beforeDestroy...');
  },
  destroyed:function()
  {
	  console.log('destroyed...');
  }
})
</script>
</body>
</html>

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值