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" />
相关推荐
yyt36304584115 小时前
KlineChartQuant Tooltip 高频交互 200FPS 性能优化完整复盘
前端·经验分享·性能优化·typescript·vue·交互·chrome devtools
这里是杨杨吖1 天前
SpringBoot+Vue心理健康咨询系统 附带详细运行指导视频
spring boot·vue·心理健康
爱上纯净的蓝天4 天前
使用 AtomCode 构建微前端架构:从设计到实现实战
架构·vue·实战·react·微前端·atomcode
山海云端有限公司6 天前
随机诗词API实战:Vue项目接入完整示例与5个常见坑
vue·实战·前端开发·跨域·api调用·随机诗词api
暗冰ཏོ6 天前
Spring Boot + Vue3 订单支付模块技术实现:支付单、回调验签、幂等处理与余额扣减
前端·spring boot·后端·vue·微信支付·支付宝支付
万亿少女的梦1687 天前
基于Python的高考志愿填报辅助系统设计与实现
java·spring boot·python·mysql·vue
万亿少女的梦1687 天前
基于SpringBoot与Vue的历史博物馆展品展示与游客管理系统设计
spring boot·mysql·vue·系统设计·博物馆管理系统
衍生星球7 天前
SpringBoot3 + Vue3 + 微信小程序如何学习,以及学哪些技术,组件
sql·微信小程序·uni-app·vue·springboot
志尊宝8 天前
Vue3 环境搭建 + 第一个 HelloVue 项目(零基础入门)
vue.js·node.js·vue·css3