Ref & Reactive
能用 Ref 就尽量用
- Ref:
- Pros: 显示调用 + 类型检查
- Cons:
.value
- Reactive:
- Pros:自动 Unwrap
- Cons:在类型上和一般对象没有区别 + ES6解构会失去响应性(toRefs) + 使用watch需要用箭头函数包装
Ref 自动解包
watch
直接接受 Ref 作为监听对象,回调中为解包后的值1
2
3
4const counter = ref(0)
watch(counter, count => {
console.log(count); // same as `counter.value`
})