2025年vue2和vue3区别很大吗(vue2和vue3有啥区别)

vue2和vue3区别很大吗(vue2和vue3有啥区别)lt template gt lt div gt lt h2 gt title lt h2 gt lt ul gt lt h3 gt num lt h3 gt lt button click num

大家好,我是讯享网,很高兴认识大家。



<template> <div> 
讯享网<h2>{{title}}</h2> <ul> <h3>{{num}}</h3> <button @click="num++">+</button> </ul> 
</div> </template> <script> export default { data() {
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> { title: </span>"Hello Vue!"<span style="color: rgba(0, 0, 0, 1)">, num:</span>0<span style="color: rgba(0, 0, 0, 1)"> }; 
}, methods:{
讯享网 show(){ console.log(</span>'我是show方法'<span style="color: rgba(0, 0, 0, 1)">); }, 
}, beforeCreate() {
console.log(</span>"-------beforeCreate--------"<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)"> beforeCreate钩子函数在组件创建之前被调用,该函数被调用的时候,props,data,mehtods都没有被创建 该组件的用处不是很大,但地位很高。 </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)"> console.log(</span>"data", <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.msg); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">console.log('props',this.title);</span> <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">this.show();</span> 
}, created(){
讯享网
讯享网 console.log(</span>"-------created--------"<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)"> created钩子函数是在init Injections&amp;Activitys之后被调用,此时已经完成props,data,methods的创建,所以在此处 调用这些方法或方法 模板或者DOM还没有被创建 此钩子函数重要的用途是用来向服务端获取网络请求数据 在此钩子函数之后的钩子中也能完成网络请求,但是一般都是在这里完成** </span><span style="color: rgba(0, 128, 0, 1)">*/</span> <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.show(); 
}, beforeMount(){
console.log(</span>'-------beforeMount--------'<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)"> 该钩子函数之前的环节主要工作是将数据读取后填充到模板上,之后有读到内存中暂时存储 </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)"> 
console.log(‘dom’,document.querySelector(‘h2’)); }, mounted(){
讯享网 console.log(</span>'-------mounted--------'<span style="color: rgba(0, 0, 0, 1)">); console.log(</span>'dom',document.querySelector('h2'<span style="color: rgba(0, 0, 0, 1)">)); 
}, beforeUpdate(){
console.log(</span>'---------beforeUpdate-----------'<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)"> beforeUpdate钩子函数中的特征 数据已经变了 但是页面还没来得及渲染 数据是新的,而页面是旧的 </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)"> console.log(</span>'data',<span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.num); console.log(</span>'dom',document.querySelector('h3'<span style="color: rgba(0, 0, 0, 1)">).innerHTML); 
}, updated(){
讯享网console.log(</span>'---------updated-----------'<span style="color: rgba(0, 0, 0, 1)">); console.log(</span>'data',<span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.num); console.log(</span>'dom',document.querySelector('h3'<span style="color: rgba(0, 0, 0, 1)">).innerHTML); 
}, beforeDestroy(){
console.log(</span>'-----------beforeDestroy--------------'<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)"> 此处是准备在销毁之前调用的钩子 此处特征,数据props,data,methods都可以访问,但是DOM已经被移除了 </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)"> console.log(</span>'data',<span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.num); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> console.log('dom',document.querySelector('h3').innerHTML);</span> 
}, destroyed(){
讯享网 console.log(</span>'-----------destroyed--------------'<span style="color: rgba(0, 0, 0, 1)">); console.log(</span>'data',<span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.num); 
} }; &lt;/script&gt; &lt;style&gt; &lt;/style&gt;

讯享网

小讯
上一篇 2025-04-30 10:04
下一篇 2025-06-15 18:02

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/138361.html