Vue - 生命周期

 

 

分析生命周期相关方法的执行时机

//===创建时的四个事件
beforeCreate() { // 第一个被执行的钩子方法:实例被创建出来之前执行
    console.log(this.message) //undefined
    this.show() //TypeError: this.show is not a function
    // beforeCreate执行时,data 和 methods 中的 数据都还没有没初始化
},
created() { // 第二个被执行的钩子方法
    console.log(this.message) //床前明月光
    this.show() //执行show方法
    // created执行时,data 和 methods 都已经被初始化好了!
    // 如果要调用 methods 中的方法,或者操作 data 中的数据,最早,只能在 created 中操作
},
beforeMount() { // 第三个被执行的钩子方法
    console.log(document.getElementById('h3').innerText) //{{ message }}
    // beforeMount执行时,模板已经在内存中编辑完成了,尚未被渲染到页面中
},
mounted() { // 第四个被执行的钩子方法
    console.log(document.getElementById('h3').innerText) //床前明月光
    // 内存中的模板已经渲染到页面,用户已经可以看见内容
},
//===运行中的两个事件
beforeUpdate() { // 数据更新的前一刻
    console.log('界面显示的内容:' + document.getElementById('h3').innerText)
    console.log('data 中的 message 数据是:' + this.message)
    // beforeUpdate执行时,内存中的数据已更新,但是页面尚未被渲染
},
updated() {
    console.log('界面显示的内容:' + document.getElementById('h3').innerText)
    console.log('data 中的 message 数据是:' + this.message)
    // updated执行时,内存中的数据已更新,并且页面已经被渲染
}

 

 

本文链接:https://blog.csdn.net/guorui_java/article/details/106917601

posted @ 2021-09-29 13:55  sam-zh  阅读(21)  评论(0编辑  收藏  举报