Data Properties and Methods

Data Properties

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:

  1. const app = Vue.createApp({
  2. data() {
  3. return { count: 4 }
  4. }
  5. })
  6. const vm = app.mount('#app')
  7. console.log(vm.$data.count) // => 4
  8. console.log(vm.count) // => 4
  9. // Assigning a value to vm.count will also update $data.count
  10. vm.count = 5
  11. console.log(vm.$data.count) // => 5
  12. // ... and vice-versa
  13. vm.$data.count = 6
  14. console.log(vm.count) // => 6

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.

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.

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.

Methods

To add methods to a component instance we use the methods option. This should be an object containing the desired methods:

  1. const app = Vue.createApp({
  2. data() {
  3. return { count: 4 }
  4. },
  5. methods: {
  6. increment() {
  7. // `this` will refer to the component instance
  8. this.count++
  9. }
  10. }
  11. })
  12. const vm = app.mount('#app')
  13. console.log(vm.count) // => 4
  14. vm.increment()
  15. console.log(vm.count) // => 5

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.

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:

  1. <button @click="increment">Up vote</button>

In the example above, the method increment will be called when the <button> is clicked.

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

  1. <span :title="toTitleDate(date)">
  2. {{ formatDate(date) }}
  3. </span>

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.

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 instead.

Debouncing and Throttling

Vue doesn’t include built-in support for debouncing or throttling but it can be implemented using libraries such as LodashData Properties and Methods - 图1 (opens new window).

In cases where a component is only used once, the debouncing can be applied directly within methods:

  1. <script src="https://unpkg.com/lodash@4.17.20/lodash.min.js"></script>
  2. <script>
  3. Vue.createApp({
  4. methods: {
  5. // Debouncing with Lodash
  6. click: _.debounce(function() {
  7. // ... respond to click ...
  8. }, 500)
  9. }
  10. }).mount('#app')
  11. </script>

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:

  1. app.component('save-button', {
  2. created() {
  3. // Debouncing with Lodash
  4. this.debouncedClick = _.debounce(this.click, 500)
  5. },
  6. unmounted() {
  7. // Cancel the timer when the component is removed
  8. this.debouncedClick.cancel()
  9. },
  10. methods: {
  11. click() {
  12. // ... respond to click ...
  13. }
  14. },
  15. template: `
  16. <button @click="debouncedClick">
  17. Save
  18. </button>
  19. `
  20. })