Skip to content

Commit 0956f45

Browse files
jay-esyyx990803
andauthored
fix ref unwrapping detailsclose #1539 (#318) (#319)
* fix ref unwrapping details close #1539 * fix typo (#323) Co-authored-by: Evan You <[email protected]>
1 parent 01d80e7 commit 0956f45

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

src/guide/essentials/reactivity-fundamentals.md

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -451,27 +451,39 @@ function increment() {
451451

452452
[Try it in the Playground](https://sfc.vuejs.org/#eyJBcHAudnVlIjoiPHNjcmlwdCBzZXR1cD5cbmltcG9ydCB7IHJlZiB9IGZyb20gJ3Z1ZSdcblxuY29uc3QgY291bnQgPSByZWYoMClcblxuZnVuY3Rpb24gaW5jcmVtZW50KCkge1xuICBjb3VudC52YWx1ZSsrXG59XG48L3NjcmlwdD5cblxuPHRlbXBsYXRlPlxuICA8YnV0dG9uIEBjbGljaz1cImluY3JlbWVudFwiPnt7IGNvdW50IH19PC9idXR0b24+XG48L3RlbXBsYXRlPiIsImltcG9ydC1tYXAuanNvbiI6IntcbiAgXCJpbXBvcnRzXCI6IHtcbiAgICBcInZ1ZVwiOiBcImh0dHBzOi8vc2ZjLnZ1ZWpzLm9yZy92dWUucnVudGltZS5lc20tYnJvd3Nlci5qc1wiXG4gIH1cbn0ifQ==)
453453

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:
455457

456458
```js
457459
const object = { foo: ref(1) }
458460
```
459461

462+
The following expression will **NOT** work as expected:
463+
460464
```vue-html
461-
{{ object.foo }} <!-- does NOT get unwrapped -->
465+
{{ object.foo + 1 }}
462466
```
463467

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:
465469

466470
```js
467471
const { foo } = object
468472
```
469473

470474
```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 <code v-pre>{{ }}</code> tag), so the following will render `1`:
481+
482+
```vue-html
483+
{{ object.foo }}
472484
```
473485

474-
Now `foo` will be unwrapped as expected.
486+
This is just a convenience feature of text interpolation and is equivalent to <code v-pre>{{ object.foo.value }}</code>.
475487

476488
### Ref Unwrapping in Reactive Objects \*\*
477489

0 commit comments

Comments
 (0)