Vue3 和 Vue2 的核心区别

Vue3是Vue2的升级版,不仅更快,还更好用。它解决了Vue2中一些让人头疼的问题,比如动态添加属性不响应、组件必须包在一个根元素里等等。

下面通过10个常见的对比例子,让你快速看懂Vue3到底新在哪儿、好在哪儿。

  1. 响应式系统:Object.defineProperty vs Proxy
    Vue 2 无法监听动态添加的属性(除非用 Vue.set);Vue 3 可以直接响应。
javascript 复制代码
// Vue 2 不会触发更新
this.obj.newProp = 'hello'

// Vue 2 正确方式
this.$set(this.obj, 'newProp', 'hello')

// Vue 3 直接赋值即可响应
this.obj.newProp = 'hello'
  1. Composition API(组合式 API)
javascript 复制代码
<!-- Vue 2:Options API -->
<script>
export default {
  data() {
    return { count: 0 }
  },
  methods: {
    increment() {
      this.count++
    }
  }
}
</script>

<!-- Vue 3:Composition API -->
<script setup>
import { ref } from 'vue'

const count = ref(0)
const increment = () => count.value++
</script>
  1. TypeScript 支持
javascript 复制代码
// Vue 3 + TypeScript(能更好的支持)
<script setup lang="ts">
interface Props {
  msg: string
}
const props = defineProps<Props>()
</script>

Vue 2 虽可通过 vue-class-component 或 vue-property-decorator 支持 TS,但配置复杂且类型推导弱。

  1. Fragment(多根节点)
javascript 复制代码
<!-- Vue 2 报错:必须有一个根元素 -->
<template>
  <header>Header</header>
  <main>Main</main>
</template>

<!-- Vue 3 允许多个根节点 -->
<template>
  <header>Header</header>
  <main>Main</main>
</template>
  1. Teleport(传送门)
html 复制代码
<template>
  <button @click="open = true">Open Modal</button>
  <Teleport to="body">
    <div v-if="open" class="modal">
      <p>Hello from Teleport!</p>
      <button @click="open = false">Close</button>
    </div>
  </Teleport>
</template>

<script setup>
import { ref } from 'vue'
const open = ref(false)
</script>

Vue 2 需手动操作 DOM 或使用第三方库(如 portal-vue)。

  1. Suspense(异步组件加载)
javascript 复制代码
<!-- 异步组件 -->
<!-- AsyncComponent.vue -->
<script setup>
const res = await fetch('/api/data')
const data = await res.json()
</script>

<template>
  <div>{{ data }}</div>
</template>
html 复制代码
<!-- 父组件 -->
<template>
  <Suspense>
    <template #default>
      <AsyncComponent />
    </template>
    <template #fallback>
      <div>Loading...</div>
    </template>
  </Suspense>
</template>

Vue 2 无原生 ,需自行管理 loading 状态。

  1. 全局 API 变更
javascript 复制代码
// Vue 2
Vue.component('MyButton', MyButton)
Vue.directive('focus', focusDirective)

// Vue 3
import { createApp } from 'vue'
const app = createApp(App)
app.component('MyButton', MyButton)
app.directive('focus', focusDirective)
app.mount('#app')

Vue 3 的应用实例彼此隔离,适合微前端或多实例场景。

  1. 生命周期钩子命名变化
javascript 复制代码
// Vue 2
export default {
  beforeDestroy() { /* cleanup */ },
  destroyed() { /* final */ }
}

// Vue 3(Options API 写法)
export default {
  beforeUnmount() { /* cleanup */ },
  unmounted() { /* final */ }
}

// Vue 3(Composition API)
import { onBeforeUnmount, onUnmounted } from 'vue'
onBeforeUnmount(() => { /* cleanup */ })
onUnmounted(() => { /* final */ })
  1. v-model 多绑定
javascript 复制代码
<!-- Vue 2:只能一个 v-model -->
<ChildComponent v-model="value" />

<!-- Child 内部约定 prop 名为 value,事件为 input -->

<!-- Vue 3:支持多个 -->
<ChildComponent 
  v-model:title="title" 
  v-model:content="content" 
/>

<!-- Child 内部接收:props: ['title', 'content'] -->
<!-- 更新时触发:emit('update:title', newTitle) -->
  1. 显式声明 emits(推荐)
javascript 复制代码
<!-- Vue 3 推荐写法 -->
<script setup>
const emit = defineEmits(['submit', 'cancel'])

const handleSubmit = () => emit('submit')
</script>

<!-- 或带验证 -->
<script setup>
const emit = defineEmits({
  submit: (payload) => typeof payload === 'string',
  cancel: null
})
</script>

Vue 2 中 $emit 无需声明,但不利于工具链和文档生成。

这些示例覆盖了 Vue2 和 Vue3 比较关键的差异点。通过代码对比,可以更清楚地看到 Vue3 在开发体验、性能、灵活性和工程化方面有明细的提升。

相关推荐
FRing0317 分钟前
vue3干货总结3(defineOptions(Vue3.3)、defineModel(Vue3.4)、Pinia)
开发语言·前端·vue.js·vue3
Bczheng121 分钟前
组件化服务端渲染模式SSR
前端
whitelbwwww2 小时前
CUDA C++实战--中级
开发语言·前端·c++
张紫娃2 小时前
Chrome插件-Stylus(屏蔽博客广告)
前端·chrome·stylus
Yannik2 小时前
在 Apple Silicon Mac 上用 Jenkins + Fastlane 搭建 iOS CI:从自动打包到 MR 检查流水线的完整实践
前端
Nayana2 小时前
《Web 到 HarmonyOS》-- 打包:Vite 产物 → HAP / HAR / HSP
前端
inksci2 小时前
用飞帆做了个小网页
前端·javascript·css·html
去伪存真2 小时前
被 pnpm 打包坑惨后,我彻底盘明白了硬链接与软链接
前端·electron
zhensherlock3 小时前
Protocol Launcher 系列:CodeHub 移动端 GitHub 协作利器
前端·javascript·typescript·node.js·自动化·github·js
多加点辣也没关系6 小时前
JavaScript|第40章:JavaScript 工程化实践
开发语言·javascript·ecmascript