vue3的新寫法和特性整理——二、生命周期函數的變化


1、棄用鈎子“beforeDestroy”、“destroyed”
ESlint檢查

 

2、鈎子的變化

<template>
  <div>
  </div>
</template>

<script>
export default {
  setup() {
    console.log('setup');
  },
  beforeCreate() {
    console.log('beforeCreate');
  },
  created() {
    console.log('created');
  },
  beforeMount() {
    console.log('beforeMount');
  },
  mounted() {
    console.log('mounted');
  },
  beforeUnmount() {
    console.log('beforeUnmount');
  },
  unmounted() {
    console.log('unmounted');
  },
  activated() {
    console.log('activated');
  },

  data() {
    return {
      initData: 'hello 你好'
    };
  },
};
</script>

  

 生命周期順序

 

注意:vue3中的生命周期函數,可以按需導入到組件中,且只能在 setup() 函數中使用

 

<script>
import { onMounted, onUpdated, onUnmounted } from 'vue';

export default {
  setup() {
    onMounted(() => {
      console.log('mounted!');
    });
    onUpdated(() => {
      console.log('updated!');
    });
    onUnmounted(() => {
      console.log('unmounted!');
    });
    return {};
  }
};
</script>

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM