【发布时间】:2018-02-16 16:43:03
【问题描述】:
所以我目前有:
App.html
<div>
<input on:input="debounce(handleInput, 300)">
</div>
<script>
import { debounce } from 'lodash'
export default {
data () {
name: ''
},
methods: {
debounce,
async handleInput (event) {
this.set({ name: await apiCall(event.target.value).response.name })
}
}
}
</script>
并得到错误Uncaught TypeError: Expected a function at App.debounce。这来自 Lodash,因此似乎没有通过 Svelte 的方法。
额外的额外编辑
关于我目前如何实现它的额外背景:
oncreate () {
const debounceFnc = this.handleInput.bind(this)
this.refs.search.addEventListener('input', debounce(debounceFnc, 300))
}
【问题讨论】:
-
你能详细说明你想要做什么吗?您通常不会在组件上使用
debounce方法 — 相反,您的方法之一会被去抖动 -
@RichHarris 抱歉 Rich 我的示例似乎错过了我添加的模板部分。添加了更多上下文以显示我也想要实现的目标。
-
尝试使用
import debounce from 'lodash/debounce' -
同样的错误@OluwafemiSule。我进一步编辑了额外的上下文,以展示我目前是如何实现这一目标的。我只是想知道我是否可以在模板中以“Svelte”的方式做到这一点。
-
debounce是在组件上定义的方法,所以它应该用作this.debounce(debounceFnc, 300)
标签: javascript svelte