当前位置:   article > 正文

vue3.0和vue2.0的区别_vue.js2和vue.js3.0的区别

vue.js2和vue.js3.0的区别

举例说明:

实现监听鼠标移动坐标

vue2.0写法

  1. <template>
  2. <div>
  3. <div>
  4. x:{{x}} <br>
  5. y:{{y}}
  6. </div>
  7. </div>
  8. </template>
  9. <script>
  10. export default {
  11. name: "index",
  12. data() {
  13. return {
  14. x:0,
  15. y:0
  16. };
  17. },
  18. created() {
  19. window.addEventListener("mousemove",this.update)
  20. },
  21. mounted() {},
  22. methods: {
  23. update(e){
  24. thisx=e.pageX;
  25. thisy=e.pageY;
  26. }
  27. }
  28. };
  29. </script>
  30. <style scoped lang="scss">
  31. </style>

vue3.0写法

  1. <template>
  2. <div>
  3. <div>
  4. x:{{x}} <br>
  5. y:{{y}}
  6. </div>
  7. </div>
  8. </template>
  9. <script>
  10. import {ref,computed,watch,onMounted,onUnmounted} from "vue"
  11. function userMousePosition(){
  12. const x = ref(0)
  13. const y = ref(0)
  14. function update(e) {
  15. x.value=e.pageX;
  16. y.value=e.pageY;
  17. }
  18. onMounted(()=>{
  19. window.addEventListener("mousemove",update)
  20. })
  21. }
  22. export default {
  23. setup(){
  24. const {x,y} =userMousePosition()
  25. return {x,y}
  26. }
  27. };
  28. </script>
  29. <style scoped lang="scss">
  30. </style>

vue 3.0新增:

ref reactive onMounted onUnmounted setup toRefs watchEffect

取代 data 和created声明周期函数

computed和watch按需引入

修改ref值在value里修改 例x.value

watch监听区别监听size变量值

父子传值


 

vuex新增

使用

vue-router新增

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/365706
推荐阅读