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;
},
}
};
source to share