You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/guide/essentials/reactivity-fundamentals.md
+24-2Lines changed: 24 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -240,7 +240,7 @@ function mutateDeeply() {
240
240
241
241
</div>
242
242
243
-
It is also possible to explicitly create [shallow refs](/api/reactivity-advanced.html#shallowref) and [shallow reactive objects](/api/reactivity-advanced.html#shallowreactive) where the reactivity is only tracked at the root-level, however they are typically only needed in advanced use cases.
243
+
It is also possible to explicitly create [shallow reactive objects](/api/reactivity-advanced.html#shallowreactive) where the reactivity is only tracked at the root-level, however they are typically only needed in advanced use cases.
244
244
245
245
<divclass="composition-api">
246
246
@@ -364,7 +364,7 @@ In other words, `ref()` allows us to create a "reference" to any value and pass
364
364
365
365
### Ref Unwrapping in Templates \*\*
366
366
367
-
When refs are exposed to templates and accessed on the render context, they are automatically "unwrapped" so there is no need to use `.value`. Here's the previous counter example, using `ref()` instead:
367
+
When refs are accessed as top-level properties in the template, they are automatically "unwrapped" so there is no need to use `.value`. Here's the previous counter example, using `ref()` instead:
368
368
369
369
```vue{13}
370
370
<script setup>
@@ -386,6 +386,28 @@ function increment() {
386
386
387
387
[Try it in the Playground](https://sfc.vuejs.org/#eyJBcHAudnVlIjoiPHNjcmlwdCBzZXR1cD5cbmltcG9ydCB7IHJlZiB9IGZyb20gJ3Z1ZSdcblxuY29uc3QgY291bnQgPSByZWYoMClcblxuZnVuY3Rpb24gaW5jcmVtZW50KCkge1xuICBjb3VudC52YWx1ZSsrXG59XG48L3NjcmlwdD5cblxuPHRlbXBsYXRlPlxuICA8YnV0dG9uIEBjbGljaz1cImluY3JlbWVudFwiPnt7IGNvdW50IH19PC9idXR0b24+XG48L3RlbXBsYXRlPiIsImltcG9ydC1tYXAuanNvbiI6IntcbiAgXCJpbXBvcnRzXCI6IHtcbiAgICBcInZ1ZVwiOiBcImh0dHBzOi8vc2ZjLnZ1ZWpzLm9yZy92dWUucnVudGltZS5lc20tYnJvd3Nlci5qc1wiXG4gIH1cbn0ifQ==)
388
388
389
+
Note the unwrapping only applies to top-level properties - nested access to refs will not be unwrapped:
390
+
391
+
```js
392
+
constobject= { foo:ref(1) }
393
+
```
394
+
395
+
```vue-html
396
+
{{ object.foo }} <!-- does NOT get unwrapped -->
397
+
```
398
+
399
+
We can fix that by making `foo` a top-level property:
400
+
401
+
```js
402
+
const { foo } = object
403
+
```
404
+
405
+
```vue-html
406
+
{{ foo }} <!-- properly unwrapped -->
407
+
```
408
+
409
+
Now `foo` will be wrapped as expected.
410
+
389
411
### Ref Unwrapping in Reactive Objects \*\*
390
412
391
413
When a `ref` is accessed or mutated as a property of a reactive object, it is also automatically unwrapped so it behaves like a normal property:
0 commit comments