Vue父子组件声明周期

 记录下,避免忘了,还要做

 

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Vue</title>
	<script src="./vue/vue.js"></script>
	<script src="./vue/vue-router.min.js"></script>
	<style>
	</style>
</head>
<body>
	<div id="app">
		<span @click="childmsg = 'childnewsmsg'">{{ msg }}</span>
		<cpt-child :msg="childmsg"></cpt-child>
	</div>
</body>
<script>
var child = Vue.extend({
	template: '<div>cpt-child:{{ msg }}</div>',
	props: {
		msg: String
	},
	beforeCreate: function(){
		console.log('beforecreate parent')
	},	
	created: function(){
		console.log('created parent')
	},
	beforeMount: function(){
		console.log('beforeMount parent')
	},
	mounted: function(){
		console.log('mounted parent')
	},
	beforeUpdate: function(){
		console.log('beforeUpdate parent')
	},
	updated: function(){
		console.log('updated parent')
	},
	beforeDestroy: function(){
		console.log('beforeDestroy parent')
	},
	destroyed: function(){
		console.log('destroyed parent')
	}
})
var vm = new Vue({
	el: '#app',
	data: {
		msg: 'msg',
		childmsg: 'childmsg'
	},
	watch: {
		childmsg: function(newVal,oldVal){
			console.log('watch change') 
		}
	},
	components: {
		'cpt-child': child
	},
	beforeCreate: function(){
		console.log('beforecreate child')
	},	
	created: function(){
		console.log('created child')
	},
	beforeMount: function(){
		console.log('beforeMount child')
	},
	mounted: function(){
		console.log('mounted child')
	},
	beforeUpdate: function(){
		console.log('beforeUpdate child')
	},
	updated: function(){
		console.log('updated child')
	},
	beforeDestroy: function(){
		console.log('beforeDestroy child')
	},
	destroyed: function(){
		console.log('destroyed child')
	}	
})
</script>
</html>

 这是按照渲染顺序console的结果,然后点击span后显示的结果。

结论:

先初始化父组件的数据,在初始化子组件的数据。

加载是相反的,先加载父组件,在加载子组件。

watch优先于update。

beforeUpdate是子组件优先,update是父组件优先。

总而言之,处理数据是子组件优先,加载到dom是父组件优先。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值