ref和refs
toRef的用法
规范化签名:将一个普通对象变为非响应式ref ,通过.value访问
1 2 3
| const obj = { count: 0 }; const countRef = toRef(obj); console.log(countRef.value);
|
对象属性签名:将reactive中具体某个对象双向 ref,会与源属性同步
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| const state = reactive({ foo: 1, bar: 2 })
const fooRef = toRef(state, 'foo')
fooRef.value++ console.log(state.foo)
state.foo++ console.log(fooRef.value)
|
toRefs用法:
1 2 3
| toRefs(<reactive Obj>)
toRefs 在调用时只会为源对象上可以枚举的属性创建 ref
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| const state = reactive({ foo: 1, bar: 2 })
const stateAsRefs = toRefs(state)
state.foo++ console.log(stateAsRefs.foo.value)
stateAsRefs.foo.value++ console.log(state.foo)
|