Vue 3 变量声明和调用

❌ Vue 2 风格(在 Vue 3 中类型会丢失或错误)

TypeScript 复制代码
const user = ref({})  // 类型是 Ref<{}>,内部属性无法推断

✅ Vue 3 + TS 正确写法:显式声明接口

TypeScript 复制代码
interface User {
  id: number
  name: string
  list?: T[]  // 可选属性
}

const name = ref('')
const count = ref<number | null>(null)        // 不确定默认值的数字类型
const user = ref<User | null>(null)           // 可能为空的场景
const userList = ref<User[]>([])              // 数组场景
const userList = ref<Array<User>>([])         // 等价写法
const activeUsers = ref<User[]>([             // 数组初始有数据
  { id: 1, name: 'Tom' }
])

// 内联类型(适合简单场景)
const user = ref<{ id: number; name: string }>({ id: 0, name: '' })
const user = ref<{ id: number; name: string } | null>(null)
const userList = ref<{ id: number; name: string }[]>([])

// 赋值时类型检查
user.value = { id: 1, name: 'Tom' }  // ✅ 通过
user.value = { id: '1' }             // ❌ TS 报错:类型不匹配

// ✅ 加泛型:类型是 Ref<User | null>,可以灵活切换
const user = ref<User | null>(null)
user.value = { id: 1, name: 'Tom' }  // ✅ 通过,符合 User 类型
user.value = null                     // ✅ 也能变回 null

为什么用 User | null 而不是直接 User
// ❌ 这样写初始值麻烦,必须造个假数据
const user = ref<User>({ id: 0, name: '' })  // 被迫写无意义的默认值

// ✅ | null 表示"可能还没有",更符合业务逻辑
const user = ref<User | null>(null)  // 明确表示"未加载"

Vue 3 函数声明和调用

TypeScript 复制代码
// 普通函数:明确参数类型和返回类型
function increment(): void {
  count.value++
}

// 带参数和返回值
function add(n: number): number {
  return count.value + n
}

// 箭头函数(常用,保持 this 上下文)
const handleClick = (e: MouseEvent): void => {
  console.log(e.target)
}

// Vue 2:必须用 this
methods: {
  foo() { this.bar() },
  bar() { console.log(this.count) }
}

// Vue 3:直接调用,无 this
function foo() { bar() }
function bar() { console.log(count.value) }

Vue 3 父子组件通信

TypeScript 复制代码
父传子(Props)

// 父组件(一样)
<Child :title="pageTitle" :user="userInfo" />

// 子组件
const props = defineProps<{
  title: string
  user: { id: number; name: string }
}>()

// 或带默认值
const props = withDefaults(defineProps<{
  title: string
  user?: { id: number; name: string }
}>(), {
  user: () => ({ id: 0, name: '' })
})

console.log(props.title)  // 直接访问,无 this


子传父(Events)

// 子组件
const emit = defineEmits<{
  update: [data: { id: number; name: string }]
  'update:modelValue': [value: string]  // v-model
}>()
事件 作用 父组件写法
update 子组件通知父组件"数据更新了" @update="handleUpdate"
update:modelValue 子组件直接修改父组件的 v-model 值 v-model="formData"

关键区别

| 特性 | @update 事件 | v-model |
| 方向 | 子 → 父(通知) | 子 ↔ 父(双向) |
| 父组件操作 | 需手动处理回调 | 自动同步 |
| 事件名 | 自定义 | 固定写法 update:modelValue |

使用场景 复杂逻辑、需确认 表单输入、简单同步
TypeScript 复制代码
emit('update', { id: 1, name: 'Tom' })

// 父组件
<Child @update="handleUpdate" v-model="formData" />
相关推荐
RuoyiOffice2 小时前
SpringBoot+Vue3+Uniapp实现PC+APP双端考勤打卡设计:GPS围栏/内网双模打卡、节假日方案、定时预生成——附数据结构和核心源码讲解
java·spring·小程序·uni-app·vue·产品运营·ruoyi
Irene19912 小时前
Vue 2、Vue 3 、Vuex 3、Vuex 4 和 Pinia 响应式丢失场景及解决方案
vue·pinia·vuex
Java陈序员1 天前
自建 Claude Code 镜像!一站式开源中转服务!
docker·node.js·vue·claude·claude code
呆头鸭L1 天前
Electron进程通信
前端·javascript·electron·前端框架·vue
木斯佳1 天前
前端八股文面经大全: 蓝色光标前端一面OC(2026-03-23)·面经深度解析
前端·面试·vue·校招·js·面经
蜡台2 天前
SPA(Single Page Application) Web 应用(即单页应用)架构模式 更新
前端·架构·vue·react·spa·spa更新
Betelgeuse762 天前
Django 项目远程服务器部署教程:从开发到生产
python·django·vue
初级见习猿工2 天前
使用pdfjs-dist在Vue 3中实现PDF文件浏览器预览
javascript·vue·pdfjs-dist
A_nanda2 天前
ZR.Admin.NET后台管理系统
vue·.net·zradmin