vue系列:父子组件生命周期的执行顺序
2023-09-27 14:27:09 时间
结论
父子组件加载过程
->父beforeCreate -> 父created -> 父beforeMount
->子beforeCreate -> 子created -> 子beforeMount -> 子mounted
-> 父mounted
子组件更新(父组件给子组件传入新的 props 时)
->父beforeUpdate
-> 子beforeUpdate -> 子updated
-> 父updated
父组件销毁
-> 父beforeDestroy
-> 子beforeDestroy -> 子destroyed
-> 父destroyed
测试
<!DOCTYPE html>
<html>
<head>
<title></title>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.min.js"></script>
</head>
<body>
<div id="app"></div>
<script>
Vue.component('Child', {
template: '<span>{{ childMsg }}</span>',
props: {
childMsg: {
type: String,
default: ''
}
},
beforeCreate: function () {
console.log("子组件的beforeCreate")
},
created: function () {
console.log("子组件的created")
},
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")
}
})
var vm = new Vue({
el: '#app',
template: `
<div>父组件:{{ fatherMsg }}<br>子组件:<Child :childMsg="fatherMsg" /></div>
`,
data: {
fatherMsg: "father"
},
beforeCreate: function () {
console.log("父组件的beforeCreate")
},
created: function () {
console.log("父组件的created")
},
beforeMount: function () {
console.log("父组件的beforeMount")
},
mounted: function () {
console.log("父组件的mounted")
setTimeout(() => {
// 触发更新
// this.fatherMsg = '我变了'
// 触发销毁
this.$destroy()
}, 2000)
},
beforeUpdate: function () {
console.log("父组件的beforeUpdate")
},
updated: function () {
console.log("父组件的updated")
},
beforeDestroy: function () {
console.log("父组件的beforeDestroy")
},
destroyed: function () {
console.log("父组件的destroyed")
}
});
</script>
</body>
</html>
相关文章
- vue--elementui---upload(三个单独组件),formData类型
- 如何通过vue实现一款简单通用的翻页组件
- vue cli4构建基于typescript的vue组件并发布到npm
- Ant Design Vue表单验证失败
- vue中$nextTick详细讲解保证你一看就明白
- vue--实现跑马灯效果
- vue.js模板的分离写法
- vue.js子组件访问父组件
- 关于Vue非父子组件通信遇到的细节问题
- 138.(前端)数据统计显示图表——vue子组件的挂载与echarts基本效果实现
- Vue 关联关系图谱组件
- Vue引用其他组件,但组件某些部分不需要时的简单处理
- vue组件定义方式,vue父子组件间的传值
- 整理4种Vue组件通信方式
- VUE组件汇总
- 动态组件 :is与is区别 vue.js
- 【实战】Vue Element+Node.js开发企业通用管理后台系统——Vue进阶(下)
- vue实现换一批业务【WoodenFish完整版】
- 【Vue 开发实战】实战篇 # 33:更加精细化的权限设计(权限组件、权限指令)
- Vue之手写 Vuex
- Vue中的scoped的实现原理以及scoped穿透的用法
- Vue组件开发实践
- axios在vue中的简单配置与使用
- Vue.js 实用技巧(二)
- vue——代码相同,但启动后样式错乱页面不同
- vue——爷孙组件之间通信