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" />
相关推荐
Anesthesia丶6 小时前
Vite + Svelte + shadcn-svelte 最小化 Demo+Vue3语法对比总结
vue·vite·svelte·shadcn-svelte
孟郎郎6 小时前
TimeoutError: The operation was aborted due to timeout at new DOMException
ai·前端框架·npm·vue·pnpm·deepseek
lpd_lt8 小时前
AI生成Spring Boot + Vue 3 + MySQL + MyBatis-Plus的项目实战
java·spring boot·vue·ai编程
来杯@Java18 小时前
图书管理系统(基于springboot+vue前后端分离的项目)计算机毕业设计java
java·spring boot·spring·vue·毕业设计·mybatis·课程设计
华大哥1 天前
前后端分离实现五级行政区划树形菜单及设备查询管理
sqlite·vue·springboot
码界筑梦坊1 天前
282-基于Python的豆瓣音乐可视化分析推荐系统
开发语言·python·信息可视化·数据分析·flask·vue
chushiyunen1 天前
滑块验证(滑动验证)
vue
Curvatureflight3 天前
前端国际化 i18n 落地实践:语言包、动态文案和格式化问题怎么处理?
前端·c++·vue
优雅格子衫3 天前
uniapp 拍照相册选取后超级好用的裁剪组件,增加水印完全自定义
开发语言·前端·javascript·uni-app·vue
路光.3 天前
uniapp中解决webview在app中调用,有过渡空白问题,增加过渡动效
uni-app·vue·app·uniapp