首页 > 其他分享 >vue3 基础-生命周期函数

vue3 基础-生命周期函数

时间:2022-08-25 23:37:22浏览次数:90  
标签:生命 console log root 周期函数 vue3 执行 data 页面

在 vue 中, 生命周期函数可理解为 "在某个时刻, 会自动执行的函数". 先直观感受一下图示.


一共就八个:

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>vue 生命周期函数</title>
  <script src="./js/[email protected]"></script>
</head>

<body>
  <div id="root"></div>
</body>
<script>
  // 生命周期函数: 在某一个时刻会自动执行的函数
  const app = Vue.createApp({
    data() {
      return {
        message: 'hello world'
      }
    },
    methods: {
      handleItemClick() {
        alert('click');
      }
    },
    // 1. 在实例生成之前
    beforeCreate() {
      console.log('beforCreate');
    },
    // 2. 在实例生成之后
    created() {
      console.log('created');
    },
    // 3. 在组件内容被渲染到页面之前
    beforeMount() {
      console.log(document.getElementById('root').innerHTML, 'beforMount');
    },
    // 4. 在组件内容被渲染到页面之后
    mounted() {
      console.log(document.getElementById('root').innerHTML, 'mounted');;
    },
    // 5. 当 data 中的数据发生变化时, 会立即自动执行
    beforeUpdate() {
      console.log(document.getElementById('root').innerHTML, 'beforeUpdate');;

    },
    // 6. 当 data 中的数据发生变化, 页面重新渲染后后执行
    updated() {
      console.log(document.getElementById('root').innerHTML, 'updated');
    },
    // 7. 当 Vue 应用失效时, 自动执行的函数
    beforeUnmount() {
      console.log('beforUnmount');
    },
    // 8. 当 Vue 应用失效时, 且dom完全销毁的时候
    unmounted() {
      console.log('unmounted');
    },
    template: `<div v-on:click=handleItemClick>{{message}}</div>`
  })

  const vm = app.mount('#root');
</script>

</html>

再来重复一遍吧, 这个也是重在理解和能基本使用即可, 没有什么技巧的.

  • beforCreate ( ) : 在实例生成前执行

  • created ( ) : 在实例生成后执行

  • beforeMount ( ) : 在组件内容被渲染到页面之前执行

  • mounted ( ) : 在组件内容被渲染到页面之后执行

  • beforUpdate ( ): 当 data 中的数据发生变化时, 会立即自动执行

  • updated ( ) : 当 data 中的数据发生变化后, 页面重新渲染后执行

  • beforUnmount ( ) : 当 vue 应用失效时, 会自动执行

  • unmounted ( ) : 当 vue 应用失效后, 且 dom 元素完全被销毁之后执行

标签:生命,console,log,root,周期函数,vue3,执行,data,页面
From: https://www.cnblogs.com/chenjieyouge/p/16626166.html

相关文章

  • vue3 学习笔记
    watchletsum=ref('0');letperson=reactive({sex:‘女’,age:18,}) watch(sum,(oldVal,newVal)=>{console.log(oldVal,newVal);})/**监视reactive所定义的一......
  • vue3 vuex4 store的响应式取值
    场景:在页面中点击按钮,数量增加,值是存在store中的,点击事件,值没变。<scriptsetuplang="ts">import{useStore}from'@/vuex';conststore=useStore()constonSu......
  • Vue3中defineEmits、defineProps 是怎么做到不用引入就能直接用的
      最近正在将一个使用单文件组件的OptionsAPI的Vue2JavaScript项目升级为Vue3typescript,并利用CompositionAPI的优势。比如,下面这种 选项API 方式:......
  • vue3 基础-应用app和组件基本概念
    这篇主要对vue最基础的应用程序Application和组件Components进行一个简要和直观的认知,具体要分析的代码如下:<!DOCTYPEhtml><htmllang="en"><head><metac......
  • vue3.0 多环境配置
    vue3.0多环境配置最常见的多环境配置,就是开发环境配置,和生产环境配置(也就是上线的配置),很多情况下我们开发环境下的域名,和一些配置项,和我们生产模式下的不同,这个时候......
  • maven标准目录结构、 Maven生命周期
    maven标准目录结构图解:  Maven生命周期图解: ......
  • vue 每次进入页面 生命周期
    生命周期1.vue有哪些生命周期函数有8个 beforeCreate created beforeMount mounted  beforeUpdate updated beforeDestroy destroyed2.一旦进入组件或......
  • vue3父子组件传值defineProps、defineEmits、defineExpose
    一、前言本文介绍父子组件传值defineProps、defineEmits、defineExpose二、语法在scriptsetup中必须使用defineProps和defineEmitsAPI来声明props和emits,它......
  • vue3的状态管理方案pinia/类似于vue2的VueX
    pinia官网:https://pinia.vuejs.org/pinia菠萝挺不错,简单又灵活。1.安装:yarnaddpinia或者 npminstallpinia,全局加--location=global2.注册使用main.jsimport{cr......
  • Spring bean 的生命周期
    bean定义:在配置文件里面用来进行定义。bean初始化:有两种方式初始化:1.在配置文件中通过指定init-method属性来完成2.实现org.springframwork......