【发布时间】:2018-11-28 13:24:27
【问题描述】:
我正在使用 typescript 使用 vuejs 进行开发,并面临方法回调工作的问题。我基本上是在尝试通过将数据包装在去抖动函数中来更新我的数据。我正在使用来自https://www.npmjs.com/package/ts-debounce 模块的去抖功能。下面是代码示例:
import { debounce } from 'ts-debounce';
export default Vue.extend({
name: 'HelloWorld',
data() {
return {
input: '# hello',
};
},
methods: {
updateWithDebounce: debounce(function(e: any) {
this.input = e.target.value;
}, 2000),
update(e: any) {
this.input = e.target.value;
},
}
此代码在功能上有效,但因编译错误而失败:
'this' 隐式具有类型'any',因为它没有类型注释。 40 | 41 | updateWithDebounce: debounce(function(e: any) {
42 | this.input = e.target.value; | ^ 43 | }, 2000), 如果有人可以帮助我解决此错误,将不胜感激。
【问题讨论】:
-
使用类风格的 Vue 组件。 vuejs.org/v2/guide/typescript.html#Class-Style-Vue-Components 这会让 TS 看到
this是什么。
标签: typescript vue.js vuejs2 typescript-typings