|
| 1 | +# Data Properties and Methods |
| 2 | + |
| 3 | +## Data Properties |
| 4 | + |
| 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: |
| 6 | + |
| 7 | +```js |
| 8 | +const app = Vue.createApp({ |
| 9 | + data() { |
| 10 | + return { count: 4 } |
| 11 | + } |
| 12 | +}) |
| 13 | + |
| 14 | +const vm = app.mount('#app') |
| 15 | + |
| 16 | +console.log(vm.$data.count) // => 4 |
| 17 | +console.log(vm.count) // => 4 |
| 18 | + |
| 19 | +// Assigning a value to vm.count will also update $data.count |
| 20 | +vm.count = 5 |
| 21 | +console.log(vm.$data.count) // => 5 |
| 22 | + |
| 23 | +// ... and vice-versa |
| 24 | +vm.$data.count = 6 |
| 25 | +console.log(vm.count) // => 6 |
| 26 | +``` |
| 27 | + |
| 28 | +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. |
| 29 | + |
| 30 | +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](reactivity.html). |
| 31 | + |
| 32 | +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. |
| 33 | + |
| 34 | +## Methods |
| 35 | + |
| 36 | +To add methods to a component instance we use the `methods` option. This should be an object containing the desired methods: |
| 37 | + |
| 38 | +```js |
| 39 | +const app = Vue.createApp({ |
| 40 | + data() { |
| 41 | + return { count: 4 } |
| 42 | + }, |
| 43 | + methods: { |
| 44 | + increment() { |
| 45 | + // `this` will refer to the component instance |
| 46 | + this.count++ |
| 47 | + } |
| 48 | + } |
| 49 | +}) |
| 50 | + |
| 51 | +const vm = app.mount('#app') |
| 52 | + |
| 53 | +console.log(vm.count) // => 4 |
| 54 | + |
| 55 | +vm.increment() |
| 56 | + |
| 57 | +console.log(vm.count) // => 5 |
| 58 | +``` |
| 59 | + |
| 60 | +Vue automatically binds the `this` value for `methods` so that it always refers to the component instance. This ensures that a method retains the correct `this` value if it's used as an event listener or callback. You should avoid using arrow functions when defining `methods`, as that prevents Vue from binding the appropriate `this` value. |
| 61 | + |
| 62 | +Just like all other properties of the component instance, the `methods` are accessible from within the component's template. Inside a template they are most commonly used as event listeners: |
| 63 | + |
| 64 | +```html |
| 65 | +<button @click="increment">Up vote</button> |
| 66 | +``` |
| 67 | + |
| 68 | +In the example above, the method `increment` will be called when the `<button>` is clicked. |
| 69 | + |
| 70 | +It is also possible to call a method directly from a template. As we'll see shortly, it's usually better to use a [computed property](computed.html) instead. However, using a method can be useful in scenarios where computed properties aren't a viable option. You can call a method anywhere that a template supports JavaScript expressions: |
| 71 | + |
| 72 | +```html |
| 73 | +<span :title="toTitleDate(date)"> |
| 74 | + {{ formatDate(date) }} |
| 75 | +</span> |
| 76 | +``` |
| 77 | + |
| 78 | +If the methods `toTitleDate` or `formatDate` access any reactive data then it will be tracked as a rendering dependency, just as if it had been used in the template directly. |
| 79 | + |
| 80 | +Methods called from a template should not have any side effects, such as changing data or triggering asynchronous processes. If you find yourself tempted to do that you should probably use a [lifecycle hook](instance.html#lifecycle-hooks) instead. |
| 81 | + |
| 82 | +### Debouncing and Throttling |
| 83 | + |
| 84 | +Vue doesn't include built-in support for debouncing or throttling but it can be implemented using libraries such as [Lodash](https://lodash.com/). |
| 85 | + |
| 86 | +In cases where a component is only used once, the debouncing can be applied directly within `methods`: |
| 87 | + |
| 88 | +```html |
| 89 | +<script src="https://unpkg.com/lodash@4.17.20/lodash.min.js"></script> |
| 90 | +<script> |
| 91 | + Vue.createApp({ |
| 92 | + methods: { |
| 93 | + // Debouncing with Lodash |
| 94 | + click: _.debounce(function() { |
| 95 | + // ... respond to click ... |
| 96 | + }, 500) |
| 97 | + } |
| 98 | + }).mount('#app') |
| 99 | +</script> |
| 100 | +``` |
| 101 | + |
| 102 | +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: |
| 103 | + |
| 104 | +```js |
| 105 | +app.component('save-button', { |
| 106 | + created() { |
| 107 | + // Debouncing with Lodash |
| 108 | + this.debouncedClick = _.debounce(this.click, 500) |
| 109 | + }, |
| 110 | + unmounted() { |
| 111 | + // Cancel the timer when the component is removed |
| 112 | + this.debouncedClick.cancel() |
| 113 | + }, |
| 114 | + methods: { |
| 115 | + click() { |
| 116 | + // ... respond to click ... |
| 117 | + } |
| 118 | + }, |
| 119 | + template: ` |
| 120 | + <button @click="debouncedClick"> |
| 121 | + Save |
| 122 | + </button> |
| 123 | + ` |
| 124 | +}) |
| 125 | +``` |
0 commit comments