新增宏用于解构响应式对象 #695
guaijie
started this conversation in
RFC Discussions
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
在现有的语法中,我们可以通过 toRefs 去解构 reactive 对象:
name 和 age 被解构,并且是 ref 类型,这表现的得很好,且符合预期。
关键在于 restObj,它并非是一个 reactive 类型,而是由多个 ref 构成的普通对象
很可惜,js 缺乏 ‘能重写解构细节’ 的内置 symbol 。
但借助编译器,我们可以实现这一点:
同样的 :name 和 age 被解构成一个 ref 类型(这是需要的,如果将其解构成一个基础数据类型,并通过打标记的方式最后编译成 ref ,会给开发者带来心智负担)
这里最重要的是 restObj 会被转化一个 reactive 对象, 类似于 reactive({ sex: 0 })
当然,我们可以重写 defineProps 来实现对 props 的解构
restObj 同样是一个 reactive 对象,但 defineProps 太过于局限,比如:我要解构 attrs 等其他 reactive 对象时将无从下手,又会陷入解构后无法保持响应式的苦恼
Beta Was this translation helpful? Give feedback.
All reactions