使用 vue 将方法传递给 lodash 给出“预期的函数”

Ste*_*n-v 3 javascript lodash vue.js vuejs2

我正在尝试将 Vue 函数传递给 lodashthrottle 方法。难道我不应该做这样的事情吗?

当我尝试执行此操作时,出现以下错误:

观察者“查询”回调中出现错误:“TypeError:需要一个函数”

守望者

watch: {
    query() {
        throttle(this.autocomplete(), 400);
    }
}
Run Code Online (Sandbox Code Playgroud)

方法

methods: {
    autocomplete() {}
}
Run Code Online (Sandbox Code Playgroud)

即使我传递了函数引用,我仍然收到错误消息。如果我用匿名函数包装它,它不会触发:

throttle(() => { this.autocomplete(); }, 400);
Run Code Online (Sandbox Code Playgroud)

我刚刚检查过,该autocomplete函数实际上似乎确实会触发,无论它不是我顶部示例中的函数的错误。

这里出了什么问题?

编辑:

Jsfiddle: http: //jsfiddle.net/yMv7y/2780/

str*_*str 5

this.autocomplete()您传递的是(也许)的返回值undefined,而不是函数引用。如果您想执行后者,则必须省略括号:

watch: {
    query() {
        throttle(this.autocomplete, 400);
    }
}
Run Code Online (Sandbox Code Playgroud)