当前位置:
首页
文章
前端
详情

v2和v3版本区别

vue2和vue3双向数据绑定原理发生了改变
vue2 的双向数据绑定是利用ES5 的一个 API Object. defineProperty()对数据进行劫持 结合 发布订阅模式的方式来实现的。


    // 数据劫持:当访问或者设置 vm 中的成员的时候,做一些干预操作
    Object.defineProperty(vm, 'msg', {
      // 可枚举(可遍历)
      enumerable: true,
      // 可配置(可以使用 delete 删除,可以通过 defineProperty 重新定义)
      configurable: true,
      // 当获取值的时候执行
      get () {
        console.log('get: ', data.msg)
        return data.msg
      },
      // 当设置值的时候执行
      set (newValue) {
        console.log('set: ', newValue)
        if (newValue === data.msg) {
          return
        }
        data.msg = newValue
        // 数据更改,更新 DOM 的值
        document.querySelector('#app').textContent = data.msg
      }
    })

vue3 中使用了 es6 的 ProxyAPI 对数据代理。


    // 模拟 Vue 实例
    let vm = new Proxy(data, {
      // 执行代理行为的函数
      // 当访问 vm 的成员会执行
      get (target, key) {
        console.log('get, key: ', key, target[key])
        return target[key]
      },
      // 当设置 vm 的成员会执行
      set (target, key, newValue) {
        console.log('set, key: ', key, newValue)
        if (target[key] === newValue) {
          return
        }
        target[key] = newValue
        document.querySelector('#app').textContent = target[key]
      }
    })

相比于vue2.x,使用proxy的优势如下

defineProperty只能监听某个属性,不能对全对象监听
可以省去for in、闭包等内容来提升效率(直接绑定整个对象即可)
可以监听数组,不用再去单独的对数组做特异性操作 vue3.x可以检测到数组内部数据的变化

vue2和vue3定义数据变量和方法的改变
在vue2中定义数据变量是data(){},创建的方法要在methods:{}中。
而在vue3中直接在setup(){}中,在这里面定义的变量和方法因为最终要在模板中使用,所以最后都得 return。setup里面没有this。
如:

<script>
import { defineComponent, ref } from 'vue';

export default defineComponent({
  name: 'App',
  setup() {
    //使用ref,说明这个数组就是全局在面板中可以使用了
    const girls = ref(['data1','data2','data3'])
    const selectGirl = ref('2')
    //设置一个函数
    const selectGirlFun = (index: number) => {
      //改变selectGirl的值要加value
      //要得到值要加value ,这些都因为使用了ref函数
      selectGirl.value = girls.value[index]
    }
    //在标签中使用的变量要使用return
    //为什么要使用return,因为有的不需要在标签中使用的就不用return
   return{
      girls,
      selectGirl,
      selectGirlFun
    }
  },
});
</script>

默认进行懒观察(lazy observation)。
在 2.x 版本里,不管数据多大,都会在一开始就为其创建观察者。当数据很大时,这可能会在页面载入时造成明显的性能压力。3.x 版本,只会对「被用于渲染初始可见部分的数据」创建观察者,而且 3.x 的观察者更高效。

更精准的变更通知。
比例来说:2.x 版本中,使用 Vue.set来给对象新增一个属性时,这个对象的所有 watcher都会重新运行;3.x 版本中,只有依赖那个属性的 watcher才会重新运行。

vue2和vue3生命周期钩子函数的不同

Vue2--------------vue3
beforeCreate  -&gt; setup()
created       -&gt; setup()
beforeMount   -&gt; onBeforeMount
mounted       -&gt; onMounted
beforeUpdate  -&gt; onBeforeUpdate
updated       -&gt; onUpdated
beforeDestroy -&gt; onBeforeUnmount
destroyed     -&gt; onUnmounted
activated     -&gt; onActivated
deactivated   -&gt; onDeactivated
errorCaptured -&gt; onErrorCaptured

vue2中的生命周期

beforeCreate 组件创建之前
created 组件创建之后
beforeMount 组价挂载到页面之前执行
mounted 组件挂载到页面之后执行
beforeUpdate 组件更新之前
updated 组件更新之后

vue3中的生命周期

setup 开始创建组件
onBeforeMount 组价挂载到页面之前执行
onMounted 组件挂载到页面之后执行
onBeforeUpdate 组件更新之前
onUpdated 组件更新之后

而且Vue3.x 生命周期在调用前需要先进行引入。
如:

import {
 reactive,
 toRefs,
 onMounted,
 onBeforeMount,
 onBeforeUpdate,
 onUpdated,
} from "vue";

部分命令发生了变化:

下载安装 npm install -g vue@cli
删除了vue list
创建项目 vue create
启动项目 npm run serve

默认项目目录结构也发生了变化:
移除了配置文件目录,configbuild文件夹 移除了 static文件夹,新增 public文件夹,并且 index.html 移动到 public中 在 src 文件夹中新增了 views 文件夹,用于分类 视图组件 和 公共组件

3.0 新加入了 TypeScript 以及 PWA 的支持

免责申明:本站发布的内容(图片、视频和文字)以转载和分享为主,文章观点不代表本站立场,如涉及侵权请联系站长邮箱:xbc-online@qq.com进行反馈,一经查实,将立刻删除涉嫌侵权内容。