实例1:
代码:
<template>
<div class="index-wrap">
<el-input v-model="message" placeholder="请输入内容"></el-input>
<p>{{ message }}</p>
</div>
</template>
<script>
export default {
data() {
return {
message: 'Today is Friday! ha-ha'
}
},
methods: {
},
beforeCreate: function() {
console.group('beforeCreate 创建前状态===============》')
console.log('%c%s', 'color:red', 'el : ' + this.$el) //undefined
console.log('%c%s', 'color:red', 'data : ' + this.$data) //undefined
console.log('%c%s', 'color:red', 'message: ' + this.message)
console.log('el.innerHTML : ' + this.$el.innerHTML)
},
created: function() {
console.group('created 创建完毕状态===============》')
console.log('%c%s', 'color:red', 'el : ' + this.$el) //undefined
console.log('%c%s', 'color:red', 'data : ' + this.$data) //已被初始化
console.log('%c%s', 'color:red', 'message: ' + this.message) //已被初始化
console.log('el.innerHTML : ' + this.$el.innerHTML)
},
beforeMount: function() {
console.group('beforeMount 挂载前状态===============》')
console.log('%c%s', 'color:red', 'el : ' + (this.$el)) //已被初始化
console.log(this.$el)
console.log('%c%s', 'color:red', 'data : ' + this.$data) //已被初始化
console.log('%c%s', 'color:red', 'message: ' + this.message) //已被初始化
console.log('el.innerHTML : ' + this.$el.innerHTML)
},
mounted: function() {
console.group('mounted 挂载结束状态===============》')
console.log('%c%s', 'color:red', 'el : ' + this.$el) //已被初始化
console.log(this.$el)
console.log('%c%s', 'color:red', 'data : ' + this.$data) //已被初始化
console.log('%c%s', 'color:red', 'message: ' + this.message) //已被初始化
console.log('el.innerHTML : ' + this.$el.innerHTML)
},
beforeUpdate: function() {
console.group('beforeUpdate 更新前状态===============》')
console.log('%c%s', 'color:red', 'el : ' + this.$el)
console.log(this.$el)
console.log('%c%s', 'color:red', 'data : ' + this.$data)
console.log('%c%s', 'color:red', 'message: ' + this.message)
console.log('el.innerHTML : ' + this.$el.innerHTML)
},
updated: function() {
console.group('updated 更新完成状态===============》')
console.log('%c%s', 'color:red', 'el : ' + this.$el)
console.log(this.$el)
console.log('%c%s', 'color:red', 'data : ' + this.$data)
console.log('%c%s', 'color:red', 'message: ' + this.message)
console.log('el.innerHTML : ' + this.$el.innerHTML)
},
beforeDestroy: function() {
console.group('beforeDestroy 销毁前状态===============》')
console.log('%c%s', 'color:red', 'el : ' + this.$el)
console.log(this.$el)
console.log('%c%s', 'color:red', 'data : ' + this.$data)
console.log('%c%s', 'color:red', 'message: ' + this.message)
console.log('el.innerHTML : ' + this.$el.innerHTML)
},
destroyed: function() {
console.group('destroyed 销毁完成状态===============》')
console.log('%c%s', 'color:red', 'el : ' + this.$el)
console.log(this.$el)
console.log('%c%s', 'color:red', 'data : ' + this.$data)
console.log('%c%s', 'color:red', 'message: ' + this.message)
console.log('el.innerHTML : ' + this.$el.innerHTML)
}
}
</script>
<style scoped>
</style>
只刷新,不改变数据时:
输入1之后:(注意观察,更新前没有1,更新后的打印值有1了)
资料:
实例2:
实例3:
代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<style>
</style>
</head>
<body>
<div id="app">
<h1>{{data}}</h1>
</div>
</body>
<script>
var myVue = new Vue({
el: "#app",
data: {
data: "aaaaa",
info: "nono"
},
beforeCreate: function() {
console.log("创建前========")
console.log(this.data)
console.log(this.$el)
},
created: function() {
console.log("已创建========")
console.log(this.info)
console.log(this.$el)
},
beforeMount: function() {
console.log("mount之前========")
console.log(this.info)
console.log(this.$el)
},
mounted: function() {
console.log("mounted========")
console.log(this.info)
console.log(this.$el)
},
beforeUpdate: function() {
console.log("更新前========");
},
updated: function() {
console.log("更新完成========");
},
beforeDestroy: function() {
console.log("销毁前========")
console.log(this.info)
console.log(this.$el)
},
destroyed: function() {
console.log("已销毁========")
console.log(this.info)
console.log(this.$el)
}
})
</script>
</html>
将上面的代码复制粘贴运行。在控制台可以看见创建前后,和挂载前后:
我们可以看见。挂载前data是虚拟的,挂载后才是真实的data
然后我们在控制台输入:myVue.data='bbb' 促使数据更新,然后再看控制台:
页面上之前是a更新后变成了b
然后我们继续输入:myVue.$destroy()再看控制台:可以看到销毁前后:
资料:
Vue生命周期 - 浅白 - 博客园https://www.cnblogs.com/gagag/p/6246493.html