Vue2.js - method call inside object and callback (this problem)

Context

I am using Vue2.js

to implement pickerOptions to preselect dateRange. I created a function createDate

that does what it sounds like. The component DatePicker

comes from here

Problem

I don't know how to correctly call the function createdDate

inside the object pickerOptions

and in the callback functiononClick(picker)

When I do this, I got this error Uncaught TypeError: this.createdDate is not a function

. This makes sense since it this

refers to

Object {__ob__: Observer}
onClick:function onClick(picker)
text:"Last week"

      

What should I do to have access to my function createDate

or what should I call it?

export default {
         name: 'app',
         data() {
             return {
                 private: {
                     pickerOptions: {
                         shortcuts: [{
                             text: 'Last week',
                             onClick(picker) {
                                 const end = new Date();
                                 const start = this.createDate(-7);
                                 picker.$emit('pick', [start, end]);
                             }
                         }]
                     }
                 }
             }
         },
         methods: {
             createDate(days, months, years) {
                 let date = new Date();
                 date.setDate(date.getDate() + days);
                 date.setMonth(date.getMonth() + months);
                 date.setFullYear(date.getFullYear() + years);
                 return date;
             },
         }
     };

      

+3


source to share


1 answer


Capturing a link in a data function will allow you to use it further. try it



data() { 
    let self = this;
    return { 
     .... 
       const start = self.$options.methods.createDate(-7);
    }
} 

      

+2


source







All Articles