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组件中data为什么必须是一个函数
Vue.js 组件的 data 选项必须是一个函数,这是为了确保每个组件实例都有自己的独立数据副本,从而避免数据污染和确保组件的复用性和可预测性
|
12月前
|
JavaScript
Vue 综合- provide() 子或孙子组件如何调用Vue对象
Vue 综合- provide() 子或孙子组件如何调用Vue对象
51 0
|
3月前
|
JavaScript
vue组件中data为什么必须是一个函数?
vue组件中data为什么必须是一个函数?
31 1
|
4月前
|
JavaScript 前端开发 API
在VUE3的setup函数中如何引用
在VUE3的setup函数中如何引用
|
11月前
|
JavaScript
53Vue - 组件的使用(data 必须是函数)
53Vue - 组件的使用(data 必须是函数)
22 0
|
4月前
|
JavaScript
Vue Steps步骤组件用法
Vue Steps步骤组件用法
160 0
|
4月前
|
JavaScript API
Vue自定义hook函数
Vue自定义hook函数
|
4月前
|
JavaScript
vue的setup中能调用哪些生命周期
vue的setup中能调用哪些生命周期
66 3
|
4月前
|
JavaScript 前端开发
Vue中的render函数是什么?它与模板的区别是什么?
Vue中的render函数是什么?它与模板的区别是什么?
156 1
|
4月前
|
JavaScript 前端开发 API
Vue3自定义Hooks定义
Vue3自定义Hooks定义
51 0