vue - 父子组件生命周期

news/2024/10/6 1:34:47

vue - 父子组件生命周期

题目

Vue 的父组件和子组件生命周期钩子函数执行顺序?

Vue 的父组件和子组件生命周期钩子函数执行顺序可以归类为以下 4 部分:

  • 加载渲染过程
    父 beforeCreate -> 父 created -> 父 beforeMount -> 子 beforeCreate -> 子 created -> 子 beforeMount -> 子 mounted -> 父 mounted
  • 子组件更新过程
    父 beforeUpdate -> 子 beforeUpdate -> 子 updated -> 父 updated
  • 父组件更新过程
    父 beforeUpdate -> 父 updated
  • 销毁过程
    父 beforeDestroy -> 子 beforeDestroy -> 子 destroyed -> 父 destroyed

校验

代码

index

// index
<template><div class="app-container"><h6>Lifecycle</h6><div class="life">生命周期:{{ lifecycleString }}</div><div><el-button type="primary" @click="showDom = !showDom">show - {{ showDom }}</el-button></div><ParentDom v-if="showDom" @updateLifeString="updateLifeString" /></div>
</template>
<script>
import ParentDom from './Parent';
export default {components: {ParentDom},data() {return {lifecycleString: '',showDom: false}},methods: {updateLifeString(string) {this.lifecycleString = string}}
}
</script>
<style lang="scss" scoped>
.app-container{.life{margin-bottom: 20px;}
}
</style>

parent

// parent
<template><div class="app-container"><h6>parent</h6><div>Date: {{ dateString }}</div><div><el-button type="primary" @click="parentUpdate">trigger parent updated</el-button></div><ChildLife @sortChildLife="sortChildLife" /></div>
</template>
<script>
import ChildLife from './Child';
export default {components: {ChildLife},data() {return {lifecycle: [],lifecycleString: '',dateString: Date.now().toLocaleString()}},beforeCreate() {console.warn('parent beforeCreate');this.sortLife('beforeCreate')},created() {this.sortLife('created')},beforeMount() {this.sortLife('beforeMount')},mounted() {this.sortLife('mounted')},beforeUpdate() {this.sortLife('beforeUpdate')},updated() {this.sortLife('updated')},beforeDestroy() {this.sortLife('beforeDestroy')},destroyed() {this.sortLife('destroyed')},methods: {sortLife(life) {let { lifecycle } = this,string = 'Parent ' + lifeconsole.log(string)lifecycle.push(string)this.$emit('updateLifeString', lifecycle.join(' -> '))},sortChildLife(life) {let { lifecycle } = this,string = lifeconsole.log(string)lifecycle.push(string)this.$emit('updateLifeString', lifecycle.join(' -> '))},parentUpdate() {this.dateString = Date.now().toLocaleString()}}
}
</script>

child

<template><div class="app-container"><h6>Child</h6><div>Child Date: {{ dateString }}</div><div><el-button type="primary" @click="ChildUpdate">trigger Child updated</el-button></div></div>
</template>
<script>
export default {data() {return {dateString: Date.now().toLocaleString()}},beforeCreate() {console.warn('child beforeCreate')this.sortLife('beforeCreate')},created() {this.sortLife('created')},beforeMount() {this.sortLife('beforeMount')},mounted() {this.sortLife('mounted')},beforeUpdate() {this.sortLife('beforeUpdate')},updated() {this.sortLife('updated')},beforeDestroy() {this.sortLife('beforeDestroy')},destroyed() {this.sortLife('destroyed')},methods: {sortLife(life) {this.$emit('sortChildLife', 'Child ' + life)},ChildUpdate() {this.dateString = Date.now().toLocaleString()}}
}
</script>

初始生命周期

parent beforeCreate
Parent created
Parent beforeMount
child beforeCreate
Child created
Child beforeMount
Child mounted
Parent mounted

错误分析:beforeCreate生命周期methods未挂载

子组件更新过程

Child beforeUpdate
Child updated

父组件更新过程

Parent beforeUpdate
Parent updated

销毁过程

Parent beforeDestroy
Child beforeDestroy
Child destroyed
Parent destroyed

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.ryyt.cn/news/28602.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈,一经查实,立即删除!

相关文章

PXE装机

PXE(Preboot eXecution Environment)是一种用于在计算机启动时通过网络加载操作系统的技术。在Linux中,PXE通常用于设置网络引导服务器,以便在网络上的其他计算机上无需本地存储介质(如硬盘或USB驱动器)即可启动Linux操作系统。 系统装机的三种引导方式: 1.硬盘 2.光驱…

多个开源的js补环境框架测试

前言 在做js逆向时肯定会遇到补环境的情况,看到github开源了好几个补环境用的框架,这篇文章做个测试,看看哪个比较好用。https://github.com/pysunday/sdenv https://github.com/bnmgh1/node-sandbox https://github.com/ylw00/qxVm https://github.com/xuxiaobo-bobo/boda_…

GpuMalll智算云:重塑AI大模型时代的智能未来

在数字化浪潮的推动下,人工智能(AI)技术正以前所未有的速度改变着世界。而AI大模型作为这一变革的核心驱动力,正逐步成为科技创新的制高点。GpuMall智算云,作为领先的AI大模型解决方案提供商,致力于为客户提供高效、智能、安全的AI服务,共同开启智能未来。在数字化浪潮的…

学习模型训练心得1

大模型时代,多年python开发人员,多多少少得了解模型训练这块,先从学习LLaMA-Factory开启吧!!! 地址:https://colab.research.google.com/drive/1d5KQtbemerlSDSxZIfAaWXhKr30QypiK?usp=sharing&pli=1#scrollTo=kbFsAE-y5so4 打开按步骤登录google账号,就可以一步一…

conda安装和使用

官网 https://www.anaconda.com/ 一、下载 https://mirrors.tuna.tsinghua.edu.cn/anaconda/miniconda/?C=M&O=D ubuntu系统下载 Miniconda3-latest-Linux-x86_64.sh 二、安装 执行以下安装 bash Miniconda3-py38_23.1.0-1-Linux-x86_64.sh 接下来,根据提示按enter或者…

Docker 必知必会----初识

什么是Docker?Docker 是一个开源的容器管理引擎。开发者可以通过Docker直接管理应用程序所需要的容器。它的logo如下: 为什么需要Docker使用Docker主要有两个原因,1、屏蔽不同环境的硬件差异,减轻开发人员在不同环境上,为了适配环境差异所需要做的工作。如各项系统配置、…

Jira Server 不维护了,如何将 Jira 平滑迁移到阿里云云效

希望进行 Jira 迁移的企业,可以借助云效的项目协作平台 Projex 轻松实现研发流程的定制化、规范化和自动化。作者:天彤 Atlassian 在 2020 年官方发布公告,从 2021 年起停止 Jira Server 产品的销售,并且在 2024 年彻底停止 Server 端产品的服务支持,这对于国内使用 Jira …