写在setup里面
<template>
<h1>vue3生命周期</h1>
<hr>
<button @click="num++">点我加1</button>
<div>{{num}}</div>
</template>
<script>
import { onBeforeMount, onBeforeUnmount, onBeforeUpdate, onMounted, onUnmounted, onUpdated, ref } from '@vue/runtime-core';
export default {
name: "Vue3Study08Vue3",
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');
})
let num = ref(0)
return {
num
};
},
};
</script>
beforeCreate和created不能写在setup里面, 但是可以写在外面, 与setup同级