在 Vue 3 的 Composition API 中,为什么使用 ref 创建的响应式变量在模板中可以直接访问,但在 setup() 函数内部却需要通过 .value 访问?
{{ count }}
```
### **Setup Context**
- **JavaScript Engine Limitation**: `ref` is a wrapper object (e.g., `{ value: 0 }`). JavaScript engines cannot automatically recognize this as a reactive reference.
Example:
```js
setup() {
const count = ref(0);
console.log(count.value); // Must explicitly access `.value`
count.value++; // Modification requires `.value`
return { count };
}
```
---
**Key Notes:**
1. **Auto-unwrapping applies only to top-level `ref`**.
2. **Nested `ref` in reactive objects** still require `.value` in templates (since `reactive` does not auto-unwrap nested `ref`), but logic code must manually handle them.
---
**Design Philosophy**
This architecture balances template simplicity with logic clarity, avoiding ambiguity in JavaScript code. The auto-unwrapping feature enhances readability while maintaining the integrity of reactive data flow.