programing

Vue2.js - 오브젝트 내의 메서드를 호출하여 콜백합니다(이 문제).

prostudy 2022. 7. 8. 22:06
반응형

Vue2.js - 오브젝트 내의 메서드를 호출하여 콜백합니다(이 문제).

맥락

사용하고 있다Vue2.jspickerOptions를 구현하여 dateRange를 미리 선택합니다.함수를 만들었습니다.createDate그렇게 들리는군요DatePicker여기서 컴포넌트가 나옵니다.

쟁점.

함수를 호출하는 올바른 방법을 모르겠습니다.createdDate대상 내에서pickerOptions콜백 기능 내에서onClick(picker)

그럴 때 이 오류가 발생하였습니다.Uncaught TypeError: this.createdDate is not a function이게 말이 되네.this에 제공하다.

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

기능에 액세스하려면 어떻게 해야 합니까?createDate아니면 뭐라고 불러야 할까?

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;
             },
         }
     };

데이터 기능에서 참조를 가져오면 더 아래로 사용할 수 있습니다.이거 드셔보세요

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

언급URL : https://stackoverflow.com/questions/43397320/vue2-js-call-a-method-inside-a-object-and-callback-this-issue

반응형