【发布时间】:2018-01-14 12:24:48
【问题描述】:
我有一个功能(getAllData 执行外部数据查询),我需要在两种情况下调用它:安装组件时和 prop 更改时。
然而,在watch 和mounted 中使用它时,我得到了TypeError: this.getAllData is not a function。
由于methods can be called from methods,我想知道从watch 或mounted 等组件调用的方法是否如此。
我的(简化的)实例如下:
export default {
props: ['triggerReload'],
data: function () {
return {
// some variables
}
},
watch: {
triggerReload: this.getAllData()
},
methods: {
getAllData: function () {
// this function correctly fetches external data
}
},
mounted: this.getAllData()
}
我的解决方法是复制函数的代码(不是 DRY)或调用外部函数(在 Vue 实例之外定义 - 这可能也是一种反模式)编辑:这是一个组件,所以我不知道如何调用外部函数并引用实例(它不是由var vm = new Vue(...) 实例化的)
【问题讨论】:
-
你检查过
this是什么吗?
标签: javascript vue.js