Jonas
Jonas

Reputation: 7885

Vue: function not defined in create hook

I can't call function in the create hook. I always get the error 'loadEmployees is not defined'. Why is this not working?

export default {
components: {
    AppointmentSlot
},
data: function() {
    return {
        employees: [],
        appointmentData : {
            date: Number,
            employee: String,
            slotSize: Number
        }
    }
},
methods: {
    onRequestSlots() {
        console.log(this.appointmentData.date);
    },
    loadEmployees: function() {
        console.log('loading...')
    }
},
created() {
    console.log('asd');
    loadEmployees();
}
}

When I call the function on a button click and not in createit works...

Im using Nuxt.js. Does this have to do with the error?

Upvotes: 0

Views: 151

Answers (1)

tony19
tony19

Reputation: 138326

loadEmployees should be called with this:

this.loadEmployees()

new Vue({
  created() {
    this.loadEmployees();
  },
  methods: {
    loadEmployees() {
      console.log('loading employees...');
    }
  }
})
<script src="https://unpkg.com/[email protected]"></script>

Upvotes: 1

Related Questions