Vue Vue3的生命周期

news/2024/7/10 2:41:53 标签: vue

文章目录

  • 配置项的形式使用
  • 组合式API形式使用

配置项的形式使用

用一个例子来学习 vue3 的生命周期钩子
在这里插入图片描述

App.vue 中引入 Demo,并添加一个按钮来隐藏或显示

<template>
  <div>
    <Demo v-if="isShow"/><br>
    <button @click="isShow = !isShow">点我隐藏/显示</button>
  </div>
</template>

<script>
import Demo from './components/Demo'
import {ref} from 'vue'
export default {
  name: 'App',
  components: {Demo},
  setup(){
    const isShow = ref(true)

    return{
      isShow
    }
  }
}
</script>

Demo 中显示一个 sum,点击按钮 sum++,同时我们把 vue3 中的生命周期钩子列出来,并打印

<template>
  <h2>当前求和为:{{ sum }}</h2>
  <button @click="sum++">点我sum++</button>
</template>

<script>
import {ref} from 'vue'

export default {
  name: 'Demo',
  setup() {
    let sum = ref(0);

    return {
      sum,
    }
  },
  //通过配置项的形式使用生命周期钩子
  beforeCreate() {
    console.log("--beforeCreate");
  },
  created() {
    console.log("--created");
  },
  beforeMount() {
    console.log("--beforeMount");
  },
  mounted() {
    console.log("--mounted");
  },
  beforeUpdate() {
    console.log("--beforeUpdate");
  },
  updated() {
    console.log("--updated");
  },
  beforeUnmount() {
    console.log("--beforeUnmount");
  },
  unmounted() {
    console.log("--unmounted");
  }
}
</script>

刚一进页面:
在这里插入图片描述
当点击 sum++ 时:
在这里插入图片描述
当点击隐藏显示时:
在这里插入图片描述
隐藏后再点击展示:
在这里插入图片描述

组合式API形式使用

Vue3.0 中可以继续使用 Vue2.x中 的生命周期钩子,但有有两个被更名:
beforeDestroy改名为beforeUnmount
destroyed改名为unmounted

Vue3.0 也提供了 Composition AP I形式的生命周期钩子,与 Vue2.x 中钩子对应关系如下:
obeforeCreate===>setup()
created =======> setup()
beforeMount ===> onBeforeMount
mounted =======> onMounted
beforeUpdate ===> onBeforeUpdate
updated =======>onUpdated
beforeUnmount ==> onBeforeUnmount
unmounted =====> onUnmounted

<template>
  <h2>当前求和为:{{ sum }}</h2>
  <button @click="sum++">点我sum++</button>
</template>

<script>
import {onBeforeMount, onBeforeUnmount, onBeforeUpdate, onMounted, onUnmounted, onUpdated, ref} from 'vue'

export default {
  name: 'Demo',
  setup() {
    let sum = ref(0);
	console.log("--setup");
    onBeforeMount(() => {
      console.log("--onBeforeMount");
    })
    onMounted(() => {
      console.log("--onMounted");
    })
    onBeforeUpdate(() => {
      console.log("--onBeforeUpdate");
    })
    onUpdated(() => {
      console.log("--onUpdated");
    })
    onBeforeUnmount(() => {
      console.log("--onBeforeUnmount");
    })
    onUnmounted(() => {
      console.log("--onUnmounted");
    })

    return {
      sum,
    }
  },
}
</script>

一进页面:
在这里插入图片描述
点 sum++
在这里插入图片描述
点击隐藏
在这里插入图片描述
点击显示
在这里插入图片描述


http://www.niftyadmin.cn/n/1816684.html

相关文章

在asp.net中生成html文件代码

在asp.net中生成html文件代码如下&#xff1a; public static bool CreatHtmlPage(string[] strNewsHtml, string[] strOldHtml, string strModeFilePath, string strPageFilePath) { bool Flage false; StreamReader ReaderFile null; …

js生成有缩进的表格

项目中用到用了两天时间想到的&#xff0c;记录下来&#xff0c;如有更好的方法&#xff0c;留言给我&#xff0c;谢谢&#xff01; js做如下表格&#xff1a; json [{"id":302,"serviceId":15,"name":"data","type":"…

Vue 自定义hook函数

文章目录定义使用封装发ajax请求的hook函数&#xff08;ts版本&#xff09;定义 什么是hook? 。本质是一个函数&#xff0c;把 setup 函数中使用的 Composition API &#xff08;组合API&#xff09;进行了封装 。类似于 vue2.x 中的 mixin 自定义 hook 的优势 。复用代码&a…

js获取数组中的最大值最小值

遍历方法&#xff1a; var tmp [1,12,8,5];var max tmp[0];for(var i1;i<tmp.length;i){ if(max<tmp[i])maxtmp[i];}console.log(max); 使用apply方法&#xff1a; var a [1,2,3,5];console.log(Math.max.apply(null, a));//最大值console.log(Math.min.apply(null, a…

征求意见,什么主题的论坛比较吸引人?

我自己有几个论坛&#xff0c;访问量一直上不去&#xff0c;而SEO也基本上做的差不多了。可是总觉得定位不太好&#xff0c;主题也不是太明确&#xff0c;这是不是一直没法吸引人的原因呢&#xff1f;现在想征求一下各位高人的意见&#xff0c;什么主题的论坛更能吸引人气呢&am…

Vue3 toRef、toRefs

toRef 作用&#xff1a;创建一个 ref 对象&#xff0c;其 value 值指向另一个对象中的某个属性。 语法&#xff1a;const name toRef(person, name ) 应用&#xff1a;要将响应式对象中的某个属性单独提供给外部使用时 扩展&#xff1a;toRefs 与 toRef 功能一致&#xff0c…

EasyX图形库__C语言__贪吃蛇核心代码

EasyX图形库__C语言__贪吃蛇核心代码 前言&#xff1a;&#xff08;第一次博客啊&#xff0c;格式不太会&#xff09; 简介&#xff1a; 1.虽然用的是EasyX的图形库&#xff0c;但其实EGE应该也可以&#xff0c;函数功能好多都是相通的&#xff1b; 2.去掉注释&#xff0c;代…

ip的正则表达式 完美版

IP地址的长度为32位2进制&#xff0c;分为4段&#xff0c;每段8位&#xff0c;用十进制数字表示&#xff0c;每段数字范围为0~255&#xff0c;段与段之间用英文句点“.”隔开。例如&#xff1a;IP地址为10.0.0.100。 分析IP地址的每组数特点&#xff1a;百位&#xff0c;十位&…