Vue:函数未在创建挂钩中定义

时间:2018-08-30 20:47:15

标签: vue.js nuxt.js

我无法在create挂钩中调用函数。我总是收到错误“未定义loadEmployees”。为什么这不起作用?

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();
}
}

当我在按钮上单击而不是在create中调用该函数时,它将起作用...

我正在使用Nuxt.js。这与错误有关吗?

1 个答案:

答案 0 :(得分:1)

loadEmployees应该用this来调用:

this.loadEmployees()

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