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
+17-5Lines changed: 17 additions & 5 deletions
Original file line number
Diff line number
Diff line change
@@ -451,27 +451,39 @@ function increment() {
451
451
452
452
[Try it in the Playground](https://sfc.vuejs.org/#eyJBcHAudnVlIjoiPHNjcmlwdCBzZXR1cD5cbmltcG9ydCB7IHJlZiB9IGZyb20gJ3Z1ZSdcblxuY29uc3QgY291bnQgPSByZWYoMClcblxuZnVuY3Rpb24gaW5jcmVtZW50KCkge1xuICBjb3VudC52YWx1ZSsrXG59XG48L3NjcmlwdD5cblxuPHRlbXBsYXRlPlxuICA8YnV0dG9uIEBjbGljaz1cImluY3JlbWVudFwiPnt7IGNvdW50IH19PC9idXR0b24+XG48L3RlbXBsYXRlPiIsImltcG9ydC1tYXAuanNvbiI6IntcbiAgXCJpbXBvcnRzXCI6IHtcbiAgICBcInZ1ZVwiOiBcImh0dHBzOi8vc2ZjLnZ1ZWpzLm9yZy92dWUucnVudGltZS5lc20tYnJvd3Nlci5qc1wiXG4gIH1cbn0ifQ==)
453
453
454
-
Note the unwrapping only applies to top-level properties - nested access to refs will not be unwrapped:
454
+
Note the unwrapping only applies if the ref is a top-level property on the template render context. As an example, `foo` is a top-level property, but `object.foo` is not.
455
+
456
+
So given the following object:
455
457
456
458
```js
457
459
constobject= { foo:ref(1) }
458
460
```
459
461
462
+
The following expression will **NOT** work as expected:
463
+
460
464
```vue-html
461
-
{{ object.foo }} <!-- does NOT get unwrapped -->
465
+
{{ object.foo + 1 }}
462
466
```
463
467
464
-
We can fix that by making `foo` a top-level property:
468
+
The rendered result will be `[object Object]1` because `object.foo` is a ref object. We can fix that by making `foo` a top-level property:
465
469
466
470
```js
467
471
const { foo } = object
468
472
```
469
473
470
474
```vue-html
471
-
{{ foo }} <!-- properly unwrapped -->
475
+
{{ foo + 1 }}
476
+
```
477
+
478
+
Now the render result will be `2`.
479
+
480
+
One thing to note is that a ref will also be unwrapped if it is the final evaluated value of a text interpolation (i.e. a <codev-pre>{{ }}</code> tag), so the following will render `1`:
481
+
482
+
```vue-html
483
+
{{ object.foo }}
472
484
```
473
485
474
-
Now `foo` will be unwrapped as expected.
486
+
This is just a convenience feature of text interpolation and is equivalent to <codev-pre>{{ object.foo.value }}</code>.
0 commit comments