|
1 | 1 | # State and Reactivity
|
2 | 2 |
|
3 |
| -## Data Properties |
| 3 | +:::tip |
| 4 | +This section contains different content for Options API and Composition API. You can toggle between the API styles using the "API Preference" switches at the top of the left sidebar. |
| 5 | +::: |
4 | 6 |
|
5 |
| -The `data` option for a component is a function. Vue calls this function as part of creating a new component instance. It should return an object, which Vue will then wrap in its reactivity system and store on the component instance as `$data`. For convenience, any top-level properties of that object are also exposed directly via the component instance: |
| 7 | +## Declaring Reactive State |
6 | 8 |
|
7 | 9 | <div class="options-api">
|
8 | 10 |
|
| 11 | +We use the `data` option to declare reactive state of a component. The option value should be a function that returns an object. Vue will call the function when creating a new component instance, and wrap the returned object in its reactivity system. The wrapped object is stored on the component instance as `$data`. For convenience, any top-level properties of that object are also exposed directly on the component instance (`this` in methods and lifecycle hooks): |
| 12 | + |
9 | 13 | ```js
|
10 |
| -const app = Vue.createApp({ |
| 14 | +export default { |
11 | 15 | data() {
|
12 |
| - return { count: 4 } |
13 |
| - } |
14 |
| -}) |
15 |
| - |
16 |
| -const vm = app.mount('#app') |
17 |
| - |
18 |
| -console.log(vm.$data.count) // => 4 |
19 |
| -console.log(vm.count) // => 4 |
20 |
| - |
21 |
| -// Assigning a value to vm.count will also update $data.count |
22 |
| -vm.count = 5 |
23 |
| -console.log(vm.$data.count) // => 5 |
| 16 | + return { |
| 17 | + count: 1 |
| 18 | + } |
| 19 | + }, |
24 | 20 |
|
25 |
| -// ... and vice-versa |
26 |
| -vm.$data.count = 6 |
27 |
| -console.log(vm.count) // => 6 |
28 |
| -``` |
| 21 | + mounted() { |
| 22 | + console.log(this.$data.count) // => 1 |
| 23 | + console.log(this.count) // => 1 |
29 | 24 |
|
30 |
| -</div> |
31 |
| -<div class="composition-api"> |
| 25 | + // Assigning a value to this.count will also update $data.count |
| 26 | + this.count = 2 |
| 27 | + console.log(this.$data.count) // => 2 |
32 | 28 |
|
33 |
| -```js |
34 |
| -const app = Vue.createApp({ |
35 |
| - setup() { |
36 |
| - const count = Vue.ref(4) |
37 |
| - return { count } |
| 29 | + // ... and vice-versa |
| 30 | + this.$data.count = 3 |
| 31 | + console.log(this.count) // => 3 |
38 | 32 | }
|
39 |
| -}) |
40 |
| - |
41 |
| -const vm = app.mount('#app') |
| 33 | +} |
| 34 | +``` |
42 | 35 |
|
43 |
| -console.log(vm.$data.count) // => 4 |
44 |
| -console.log(vm.count) // => 4 |
| 36 | +These instance properties are only added when the instance is first created, so you need to ensure they are all present in the object returned by the `data` function. Where necessary, use `null`, `undefined` or some other placeholder value for properties where the desired value isn't yet available. |
45 | 37 |
|
46 |
| -// Assigning a value to vm.count will also update $data.count |
47 |
| -vm.count = 5 |
48 |
| -console.log(vm.$data.count) // => 5 |
| 38 | +It is possible to add a new property directly to the component instance without including it in `data`. However, because this property isn't backed by the reactive `$data` object, it won't automatically be tracked by [Vue's reactivity system](/guide/advanced/reactivity-in-depth.html). |
49 | 39 |
|
50 |
| -// ... and vice-versa |
51 |
| -vm.$data.count = 6 |
52 |
| -console.log(vm.count) // => 6 |
53 |
| -``` |
| 40 | +Vue uses a `$` prefix when exposing its own built-in APIs via the component instance. It also reserves the prefix `_` for internal properties. You should avoid using names for top-level `data` properties that start with either of these characters. |
54 | 41 |
|
55 | 42 | </div>
|
56 | 43 |
|
57 |
| -These instance properties are only added when the instance is first created, so you need to ensure they are all present in the object returned by the `data` function. Where necessary, use `null`, `undefined` or some other placeholder value for properties where the desired value isn't yet available. |
| 44 | +<div class="composition-api"> |
58 | 45 |
|
59 |
| -It is possible to add a new property directly to the component instance without including it in `data`. However, because this property isn't backed by the reactive `$data` object, it won't automatically be tracked by [Vue's reactivity system](/guide/advanced/reactivity-in-depth.html). |
60 | 46 |
|
61 |
| -Vue uses a `$` prefix when exposing its own built-in APIs via the component instance. It also reserves the prefix `_` for internal properties. You should avoid using names for top-level `data` properties that start with either of these characters. |
| 47 | + |
| 48 | +</div> |
62 | 49 |
|
63 | 50 | ## Methods
|
64 | 51 |
|
@@ -114,18 +101,17 @@ Vue doesn't include built-in support for debouncing or throttling but it can be
|
114 | 101 |
|
115 | 102 | In cases where a component is only used once, the debouncing can be applied directly within `methods`:
|
116 | 103 |
|
117 |
| -```vue-html |
118 |
| -<script src="https://unpkg.com/[email protected]/lodash.min.js"></script> |
119 |
| -<script> |
120 |
| - Vue.createApp({ |
121 |
| - methods: { |
122 |
| - // Debouncing with Lodash |
123 |
| - click: _.debounce(function() { |
124 |
| - // ... respond to click ... |
125 |
| - }, 500) |
126 |
| - } |
127 |
| - }).mount('#app') |
128 |
| -</script> |
| 104 | +```js |
| 105 | +import { debounce } from 'lodash-es' |
| 106 | + |
| 107 | +createApp({ |
| 108 | + methods: { |
| 109 | + // Debouncing with Lodash |
| 110 | + click: debounce(function () { |
| 111 | + // ... respond to click ... |
| 112 | + }, 500) |
| 113 | + } |
| 114 | +}).mount('#app') |
129 | 115 | ```
|
130 | 116 |
|
131 | 117 | However, this approach is potentially problematic for components that are reused because they'll all share the same debounced function. To keep the component instances independent from each other, we can add the debounced function in the `created` lifecycle hook:
|
|
0 commit comments