Vue对象实例从初始化到销毁的整个过程。
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>生命周期</title>
<script src="vue.js"></script>
</head>
<body>
<!-- 生命周期:vue实例从初始化到销毁的整个过程 -->
<!-- 钩子函数:在生命周期内触发的各种函数 -->
<div id="app">
<p>{{ number }}</p>
<input v-model="number" />
</div>
<script>
var app = new Vue({
el: '#app',
data: {
number: 1
},
beforeCreate: function() {
console.log('初始化之前(beforeCreate)');
console.log(' el: ' + this.$el)
console.log('data: ' + this.number)
},
created: function() {
console.log('初始化完成(created)');
console.log(' el: ' + this.$el)
console.log('data: ' + this.number)
},
beforeMount: function() {
console.log('渲染el之前(beforeMount)');
console.log(' el: ' + this.$el)
console.log('data: ' + this.number)
},
mounted: function() {
console.log('渲染el完成(beforeMount)');
console.log(' el: ' + this.$el)
console.log('data: ' + this.number)
},
beforeUpdate: function() {
console.log('更新data之前(beforeUpdate)');
console.log(' el: ' + this.$el)
console.log('data: ' + this.number)
},
updated: function() {
console.log('更新data完成(beforeUpdate)');
console.log(' el: ' + this.$el)
console.log('data: ' + this.number)
},
beforeDestroy: function() {
console.log('销毁实例之前(beforeUpdate)');
console.log(' el: ' + this.$el)
console.log('data: ' + this.number)
},
destroyed: function() {
console.log('销毁实例完成(beforeUpdate)');
console.log(' el: ' + this.$el)
console.log('data: ' + this.number)
},
})
</script>
</body>
</html>
生命周期.png
网友评论