Vue 3 核心 API 概览

Vue 3 核心 API 概览

Vue 3 引入了 Composition API 作为其核心特性之一,同时保留了 Options API。以下是 Vue 3 的主要核心 API:

1. 应用实例 API

createApp

import { createApp } from 'vue'
const app = createApp({ /* 根组件选项 */ })

应用配置

app.config
  .errorHandler = (err, vm, info) => { /* 错误处理 */ }
  .warnHandler = (msg, vm, trace) => { /* 警告处理 */ }
  .globalProperties = { /* 全局属性 */ }

应用方法

app.mount('#app') // 挂载应用
app.unmount()     // 卸载应用
app.component()    // 注册全局组件
app.directive()    // 注册全局指令
app.use()         // 使用插件

2. 组合式 API (Composition API)

响应式基础

import { ref, reactive, computed, watch, watchEffect } from 'vue'

// 基本响应式
const count = ref(0) // 适用于基本类型
const state = reactive({ count: 0 }) // 适用于对象

// 计算属性
const doubleCount = computed(() => count.value * 2)

// 侦听器
watch(count, (newVal, oldVal) => { /* ... */ })
watchEffect(() => { console.log(count.value) }) // 自动追踪依赖

生命周期钩子

import { onMounted, onUpdated, onUnmounted } from 'vue'

setup() {
  onMounted(() => { /* ... */ })
  onUpdated(() => { /* ... */ })
  onUnmounted(() => { /* ... */ })
  // 其他:onBeforeMount, onBeforeUpdate, onBeforeUnmount, onErrorCaptured
}

依赖注入

import { provide, inject } from 'vue'

// 祖先组件
setup() {
  provide('theme', 'dark')
}

// 后代组件
setup() {
  const theme = inject('theme', 'light' /* 默认值 */)
}

3. 响应式工具函数

import { 
  isRef, unref, toRef, toRefs, 
  isProxy, isReactive, isReadonly,
  markRaw, shallowRef, shallowReactive, readonly
} from 'vue'

// 转换和检查
const unwrapped = unref(maybeRef) // 解包ref
const objRef = toRef(object, 'key') // 对象属性转为ref
const refs = toRefs(reactiveObj) // 将reactive对象转换为普通对象,每个属性都是ref

// 只读和浅响应式
const readOnlyState = readonly(state)
const shallow = shallowReactive({ nested: { ... } }) // 只响应第一层

4. 组件相关 API

defineComponent

import { defineComponent } from 'vue'

export default defineComponent({
  // 组件选项
})

defineAsyncComponent (异步组件)

const AsyncComp = defineAsyncComponent(() => import('./AsyncComp.vue'))

模板引用

import { ref, onMounted } from 'vue'

setup() {
  const root = ref(null)
  
  onMounted(() => {
    console.log(root.value) // 获取DOM元素或组件实例
  })
  
  return { root }
}

5. 内置组件

<template>
  <Teleport to="body">...</Teleport>  // 传送组件
  <Suspense>                          // 异步组件处理
    <template #default>...</template>
    <template #fallback>Loading...</template>
  </Suspense>
  <KeepAlive>...</KeepAlive>          // 缓存组件
  <Transition>...</Transition>        // 过渡动画
</template>

6. 自定义渲染器 API

import { createRenderer } from '@vue/runtime-core'

const { render, createApp } = createRenderer({
  // 自定义渲染逻辑
})

7. 服务端渲染 API

import { renderToString } from '@vue/server-renderer'

const html = await renderToString(app)

8. TypeScript 支持

Vue 3 完全使用 TypeScript 重写,提供了良好的类型支持:

interface User {
  id: number
  name: string
}

const user = ref<User>({ id: 1, name: 'John' })

这些核心 API 构成了 Vue 3 的基础功能,使得开发更加灵活和高效。Composition API 特别适合复杂组件的逻辑组织和复用。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Fro.Heart

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值