Vue3.5常用特性整理

Vue3.5 发布已近半年,抽空整理下常用的新增/改动特性
响应式 Props 解构

Vue3.5 中 Props 正式支持解构了,并添加了响应式跟踪
设置默认值

使用 JavaScript 原生的默认值语法声明 props 默认值

以前

const props = withDefaults(
  defineProps<{
    count?: number
    msg?: string
  }>(),
  {
    count: 0,
    msg: 'hello'
  }

)

现在

const { count = 0, msg = 'hello' } = defineProps<{
  count?: number
  message?: string

}>()

响应式解构

当在同一个 <script setup> 代码块中访问由 defineProps 解构的变量时,Vue 编译器会自动在前面添加 props

以前

const { foo } = defineProps(['foo'])
 
watchEffect(() => {
  // 在 3.5 之前只运行一次
  console.log(foo)

})

现在

const { foo } = defineProps(['foo'])
 
watchEffect(() => {
  // 在 3.5 中在 "foo" prop 变化时重新执行
  console.log(foo)
  // `foo` 由编译器转换为 `props.foo`,以上等同于 `console.log(props.foo)`

})

与之类似,监听解构的 prop 变量 或 将其传递到可组合项中同时保留响应性 时需要将其包

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

一对一答疑的编程作家朱文伟

谢谢大爷谢谢大爷谢谢大爷谢谢大

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

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

打赏作者

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

抵扣说明:

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

余额充值