vue3.js
总结: 此处涉及的vue3新特性
vue2中生命周期使用选项式API,直接写在vue组件配置对象中
vue3中生命周期使用组合式API,写在setup函数中, 用Vue调用
vue3不再使用beforeCreate 和 created
vue3中的销毁函数由destroy 改成 unmount
<body><script src='./vue3.js'></script><div id='myApp'><button @click="count++">{{count}}</button></div><script>var vm = {data(){ return{ count: 0 } },// vue2的生命周期函数(vue3中依然可用没有废除)beforeCreate() { console.log('vue2', "beforeCreate") },created() { console.log('vue2', "created") },beforeMount() { console.log('vue2', "beforeMount") },mounted() { console.log('vue2', "mounted")// this.$destroy(); vue3废除了$destroy函数},beforeUpdate() { console.log('vue2', "beforeUpdate") },updated() { console.log('vue2', "updated") },beforeDestroy() { console.log('vue2', "beforeDestroy") },destroyed() { console.log('vue2', "destroyed") },setup() {// setup函数在beforeCreate生命周期钩子之前调用, 不能用thisconsole.log('setup', this) // setup window// vue3建议在setup中实现生命周期函数调用, 如下:// beforeCreate 和 created 在vue3已经不再使用Vue.onBeforeMount(()=>{console.log("vue3", "onBeforeMount")})Vue.onMounted(() => {console.log("vue3", "onMounted")})Vue.onBeforeUpdate(() => {console.log("vue3", "onBeforeUpdate")})Vue.onUpdated(() => {console.log("vue3", "onUpdated")})Vue.onBeforeUnmount(() => {console.log("vue3", "onBeforeUnmount")})Vue.onUnmounted(() => {console.log("vue3", "onUnmounted")})}}Vue.createApp(vm).mount('#myApp')// vue3中不能使用vm调用组件中的数据了console.log(vm.count, vm.$data)</script></body>
