在reactive中定义一个ref类型的属性是否没有意义 #11468
Unanswered
jasonrob-wjy
asked this question in
Help/Questions
Replies: 1 comment
-
一般情况下,在响应式变量中放入响应式变量是无意义的,因为你总能通过最外层的proxy完成对最内层的更新。 通常在一些需要监听多个响应式数据的情况下有用: const a: Ref<any> = useA()
const b: Ref<any> = useB()
const c: Ref<any> = useC()
// watch([a,b,c], ([valA,valB,valC])=>{})
const data = reactive({a,b,c})
watch(data, (val) => {}) 更具体的说,在你写一个接受很多响应式数据的选项的函数时: function useUwu(options: Record<string, Ref<any>> = {}) {
// const { a,b,c,d } = options
const {
other,
...restReactiveOptions
} = options
const watchTarget = reactive(restReactiveOptions)
watch(watchTarget, () => {})
} |
Beta Was this translation helpful? Give feedback.
0 replies
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.
-
在reactive中声明普通类型的属性:
const state1 = reactive({ count: 0 })
在reactive中声明ref类型的属性:
const state2 = reactive({ count: ref(0) })
1.由于reactive的深层响应式,只要state1.count变化,会触发响应式更新,而state2.count也是同样的效果
2.数据访问时,由于ref的自动解包机制,可以通过state1.count访问,state2.count也是同样的效果
所以我的疑问是在reactive中定义一个ref类型的属性是否没有意义,如果有意义的话,是什么应用场景
Beta Was this translation helpful? Give feedback.
All reactions