3

Context

I'm using Vue2.jsto implement pickerOptions to preselect dateRange. I created a function createDate that does what it sounds. The DatePicker component comes from here

Issue

I don't know the right way to call the function createdDate within the object pickerOptions and within the callback function onClick(picker)

When I do so I got this error Uncaught TypeError: this.createdDate is not a function. This makes sense since this reffers to

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

What should I do to be able to access my function createDate or how to 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;
             },
         }
     };

1 Answer 1

2

Grabbing a reference in the data function should let you use it further down. Try this

data() { 
    let self = this;
    return { 
     .... 
       const start = self.$options.methods.createDate(-7);
    }
} 
Sign up to request clarification or add additional context in comments.

1 Comment

This did it : self.$options.methods.createDate. You can update your answer. Thank you !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.