Vue3基础(十yi)___常用生命周期函数___setup___onMounted___onUpdated

简介: 本文介绍了Vue 3中的常用生命周期函数,包括`setup`、`onBeforeMount`、`onMounted`、`onBeforeUpdate`、`onUpdated`、`onBeforeUnmount`和`onUnmounted`,并解释了它们与Vue 2生命周期钩子的对应关系。文章通过代码示例展示了这些生命周期钩子在组件中的使用时机和场景。

对比vue2

beforeCreate -> 使用setup()
created -> 使用 setup()
beforeMount -> onBeforeMount
mounted -> onMounted
beforeUpdate -> onBeforeUpdate
updated -> onUpdated
beforeDestroy -> onBeforeUnmount
destroyed -> onUnmounted
errorCaptured -> onErrorCaptured

setup

setup处于beforeCreate之前,beforeCreate和created都将使用setup代替,里面没有this指向,没有data,没有methods,组合Api最终编译为options Api。

onBeforeMount

onBeforeMount页面挂在之前;

onMounted

onMounted页面初始化完毕,一般进行ajax获取数据;

onBeforeUpdate

onBeforeUpdate页面进行更新之前;

onUpdated

onUpdated页面更新完成之后;

onBeforeUnmount

onBeforeUnmount页面销毁之前;

onUnmounted

onUnmounted页面销毁;

首先下载vue-router 我们在进行页面跳转的时候会使用到;
在这里插入图片描述
在main中进行挂载全局 router

import {
    createApp } from 'vue'
import App from './App.vue'
import './index.css'
import {
    createRouter, createWebHashHistory } from 'vue-router'
const HelloWorld = () => import("./components/HelloWorld.vue")
const home = () => import("./components/home.vue")
const router = createRouter({
   
    history: createWebHashHistory(),
    routes: [
    {
    path: '/hellow', component: HelloWorld }, 
    {
    path: '/', component: home}
    ],
})
createApp(App).use(router).mount('#app')

/home

<template>
  <div>
    <p>age:{
   {
    strObj.age }}</p>
    <button @click="changeAgeFun">changeAgeFun按钮</button>
    <button @click="goHellowPages">跳转到hellow页面</button>
  </div>
</template>

<script>
import {
   
  ref,
  onBeforeMount,
  onMounted,
  onBeforeUpdate,
  onUpdated,
  onBeforeUnmount,
  onUnmounted,
} from "vue";
import {
    useRouter } from "vue-router";
export default {
   
  name: "App",
  setup() {
   
    let strObj = ref({
    age: 18 });
    onBeforeMount(() => {
   
      console.log("onBeforeMount===");
    });
    onMounted(() => {
   
      console.log("onMounted===");
    });
    onBeforeUpdate(() => {
   
      console.log("onBeforeUpdate===");
    });
    onUpdated(() => {
   
      console.log("onUpdated===");
    });
    onBeforeUnmount(() => {
   
      console.log("onBeforeUnmount===");
    });
    onUnmounted(() => {
   
      console.log("onUnmounted===");
    });
    const changeAgeFun = () => {
   
      strObj.value.age++;
      console.log("年龄进行加,页面发生改变===");
    };
    const router = useRouter();
    const goHellowPages = () => {
   
      console.log("进行路由跳转===");
      router.push("/hellow");
    };
    return {
    strObj, changeAgeFun, goHellowPages };
  },
};
</script>

/hellow

<template>
  <h1>我是hellow组件</h1>
</template>
<script>
export default {
   
}
</script>

首先点击改变年龄按钮,让年龄改变,出发页面的更新,随后进行页面的跳转,销毁当前实例
在这里插入图片描述

目录
相关文章
|
4月前
|
JavaScript 前端开发 安全
Vue 3
Vue 3以组合式API、Proxy响应式系统和全面TypeScript支持,重构前端开发范式。性能优化与生态协同并进,兼顾易用性与工程化,引领Web开发迈向高效、可维护的新纪元。(238字)
715 139
|
4月前
|
缓存 JavaScript 算法
Vue 3性能优化
Vue 3 通过 Proxy 和编译优化提升性能,但仍需遵循最佳实践。合理使用 v-if、key、computed,避免深度监听,利用懒加载与虚拟列表,结合打包优化,方可充分发挥其性能优势。(239字)
361 1
|
5月前
|
开发工具 iOS开发 MacOS
基于Vite7.1+Vue3+Pinia3+ArcoDesign网页版webos后台模板
最新版研发vite7+vue3.5+pinia3+arco-design仿macos/windows风格网页版OS系统Vite-Vue3-WebOS。
556 11
|
4月前
|
JavaScript 安全
vue3使用ts传参教程
Vue 3结合TypeScript实现组件传参,提升类型安全与开发效率。涵盖Props、Emits、v-model双向绑定及useAttrs透传属性,建议明确声明类型,保障代码质量。
432 0
|
6月前
|
缓存 前端开发 大数据
虚拟列表在Vue3中的具体应用场景有哪些?
虚拟列表在 Vue3 中通过仅渲染可视区域内容,显著提升大数据列表性能,适用于 ERP 表格、聊天界面、社交媒体、阅读器、日历及树形结构等场景,结合 `vue-virtual-scroller` 等工具可实现高效滚动与交互体验。
618 1
|
6月前
|
缓存 JavaScript UED
除了循环引用,Vue3还有哪些常见的性能优化技巧?
除了循环引用,Vue3还有哪些常见的性能优化技巧?
352 0
|
7月前
|
JavaScript
vue3循环引用自已实现
当渲染大量数据列表时,使用虚拟列表只渲染可视区域的内容,显著减少 DOM 节点数量。
168 0
|
5月前
|
JavaScript
Vue中如何实现兄弟组件之间的通信
在Vue中,兄弟组件可通过父组件中转、事件总线、Vuex/Pinia或provide/inject实现通信。小型项目推荐父组件中转或事件总线,大型项目建议使用Pinia等状态管理工具,确保数据流清晰可控,避免内存泄漏。
471 2
|
4月前
|
缓存 JavaScript
vue中的keep-alive问题(2)
vue中的keep-alive问题(2)
404 137
|
8月前
|
人工智能 JavaScript 算法
Vue 中 key 属性的深入解析:改变 key 导致组件销毁与重建
Vue 中 key 属性的深入解析:改变 key 导致组件销毁与重建
934 0