代码之家  ›  专栏  ›  技术社区  ›  Abhijit Mondal Abhi

如何在vuejs的回调函数中存储变量中的值

  •  0
  • Abhijit Mondal Abhi  · 技术社区  · 4 年前

      props: {
                startDate: Date,            // declare prop here
                expertId: Number | String,
                leadToOpen: Number | String,
                config: Object,
                defaultView: {
                    type: String,
                    default: "timeGridWeek",
                },
                header: {
                    type: Object,
                    default() {
                        return {
                            left: "title",
                            center: "timeGridWeek dayGridMonth dayGrid",
                            right: "today prev,next",
                        };
                    },
                },
                goTo: {
                    type: Date,
                    default: null,
                },
            },
    
    
    data() {
        return {
            events: {
                type: Array,
                default(){
                    return{
                        id: 'a',
                        title: 'my event',
                        start: '2020-10-10'
                    }
                }
            },
            busy: false,
            displayAppointment: null,
            displayOwner: null,
            eventTypes: [
                { name: "Kundentermin", color: "#32bb60" },
                { name: "Termin bei Lead", color: "#db0630" },
                { name: "Termin ohne Kunde/Lead", color: "#3f888f" },
                { name: "Privater Termin (akzeptiert)", color: "#4682B4" },
                { name: "Privater Termin (offen)", color: "#505050" },
                { name: "Ehemaliger Termin", color: "#cdcdcd" },
            ],
            showEdit: false,
            showInfo: false,
            showModal: false,
            leadId: null,
            locale: "de",
            locales: [deLocale],
            calendarOptions: {
                headerToolbar: this.header,
                plugins: [dayGridPlugin, timeGridPlugin],
                initialView: "timeGridWeek",
                eventClick: this.eventClickHandler,
                events: null,                
                slotMinTime: "07:00:00",
                slotMaxTime: "21:00:00",
                locale: "de",
                locales: [deLocale],
                ref: "calendar",
                eventDisplay: "block",
                displayEventTime: false,
                height: "auto",
                allDaySlot: false,
                buttonText: {
                    dayGrid: "Tag",
                },
                lazyFetching: true,
                datesSet: function (dateInfo) {   /////// here is the call back function ////////
                  this.startDate = dateInfo.start;
                  console.log( this.startDate);
                }
            },
    
        };
    },
    
    
      computed: {
          dateRange: function(){
           return this.startDate;   // undefined
          }
        },
    
    2 回复  |  直到 4 年前
        1
  •  2
  •   tao    4 年前

    问题是回调函数。如果您想访问Vue实例,它必须是 arrow function :

    dateSet: (dateInfo) => {
      this.startDate = dateInfo.start
    }
    

    在你的电话里, this

        2
  •  1
  •   Loki    4 年前

    你能试试:

    computed: {
       getDateRange() {
            return this.startDate;
       }
    }