vue组件的生命周期 笔记

beforeCreate
1.组件创建实例
created(重要:1.发送网络请求 2.事件监听 3.this.$wacth())
2.template模板编译
beforeMount
3.挂载到虚拟DOM - 虚拟DOM - 真实的DOM - 界面看到的h2/div等
mounted(重要:元素已经挂载 获取DOM 使用DOM)
4.数据更新:message改变
beforeUpdate
根据最新的数据生成新的VNode,生成新的虚拟DOM 真实DOM
updated
5.不再使用 v-if=“false”
beforeUnmount
将之前挂载在虚拟DOM中VNode从虚拟DOM移除
unmounted(相对重要:回收操作(取消事件监听))
将组件实例销毁

app.vue 代码

<template>
  <div>
    <div>哈哈哈</div>
    <h1>{{ message }} {{ count }}</h1>
    <button @click="count++">按钮+1</button>

    <home v-if="isShowHome"/>
    <button @click="isShowHome = !isShowHome">显示组件</button>
  </div>
</template>
<script>
import home from "./home.vue"
export default {
    components:{
        home
    },
  data() {
    return {
      message: "Hello App",
      count: 0,
      isShowHome:true
    };
  },
  beforeCreate() {
    console.log("beforeCreate");
  },
  created() {
    console.log("created");
    console.log("发送网络请求 2.事件监听 3.this.$wacth()");
  },
  beforeMount() {
    console.log("beforeMount");
  },
  mounted() {
    console.log("mounted");
  },
  beforeUpdate() {
    console.log("beforeUpdate");
  },
  updated() {
    console.log("updated");
  },
};
</script>
<style scoped>
</style>

home 组件

<template>

       <h2>我是home组件</h2>
</template>
<script>
export default {
  data() {
    return {};
  },
  beforeUnmount(){
    console.log(' home beforeUnmount');
  },
  unmounted(){
    console.log('home unmounted');
  }
};
</script>
<style scoped>
</style>

你可能感兴趣的:(vue.js,笔记,前端)