Skip to content

Commit cdaf92e

Browse files
committed
add code examples to components one-way data flow
1 parent bf09935 commit cdaf92e

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

src/guide/components.md

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -357,9 +357,25 @@ There are usually two cases where it's tempting to mutate a prop:
357357

358358
The proper answer to these use cases are:
359359

360-
1. Define a local data property that uses the prop's initial value as its initial value;
360+
1. Define a local data property that uses the prop's initial value as its initial value:
361361

362-
2. Define a computed property that is computed from the prop's value.
362+
``` js
363+
props: ['initialCounter'],
364+
data: function () {
365+
return { counter: this.initialCounter }
366+
}
367+
```
368+
369+
2. Define a computed property that is computed from the prop's value:
370+
371+
``` js
372+
props: ['size'],
373+
computed: {
374+
normalizedSize: function () {
375+
return this.size.trim().toLowerCase()
376+
}
377+
}
378+
```
363379

364380
<p class="tip">Note that objects and arrays in JavaScript are passed by reference, so if the prop is an array or object, mutating the object or array itself inside the child **will** affect parent state.</p>
365381

@@ -1013,7 +1029,7 @@ When the `inline-template` special attribute is present on a child component, th
10131029
<div>
10141030
<p>These are compiled as the component's own template.</p>
10151031
<p>Not parent's transclusion content.</p>
1016-
</div>
1032+
</div>
10171033
</my-component>
10181034
```
10191035

0 commit comments

Comments
 (0)