Data Properties and Methods

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

    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. const vm = app.mount('#app')
    12. console.log(vm.count) // => 4
    13. vm.increment()
    14. 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:

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

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

    If the methods toTitleDate or 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.

    Vue doesn’t include built-in support for debouncing or throttling but it can be implemented using libraries such as .

    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. unmounted() {
    6. // Cancel the timer when the component is removed
    7. this.debouncedClick.cancel()
    8. },
    9. methods: {
    10. click() {
    11. // ... respond to click ...
    12. }
    13. },
    14. template: `
    15. <button @click="debouncedClick">
    16. Save
    17. </button>