gpt4 book ai didi

typescript - 如何使用 jsDoc 在自定义组件中记录此关键字?

转载 作者:行者123 更新时间:2023-12-03 06:36:14 25 4
gpt4 key购买 nike

我正在使用 Vuejs 内联模板组件,我们在 javascript 文件中注册组件,在 html 中注册模板。
该组件看起来像这样:

Vue.component('compare-benefits', {
data() {
// the "this" keyword in methods should refer to this object
return {
...state,
isLoading: false,
}
},
methods: {
getData() {
// I want the "this" keyword here to reference the object return in data above
this.isLoading = true;
}
}
})
如果你不熟悉 Vue,这里发生的事情是 Vue 框架将绑定(bind) this方法中的关键字指向从 data() 方法返回的对象。
我如何在这里使用 jsDoc 并告诉它 this这里的关键字实际上是在引用那个对象?
编辑 : 使用 jsDoc 的主要原因不是创建文档,而是使用 @ts-check 在 vscode 中进行智能感知和类型检查。

最佳答案

this Vue framework 中的关键字是 Vue 类型的对象.您可以通过在 getData 中调试代码来识别它。方法或任何其他方法。此外,Vue datathis 的属性.我在下面上传了一个屏幕截图,供您从我目前正在处理的一个示例中查看:
enter image description here
结果,你使用 jsDoc 后的代码会是这样的:

Vue.component('compare-benefits', {
data() {
return {
...state,
isLoading: false,
}
},
methods: {
/** @this Vue */
getData() {
this.isLoading = true;
}
}
})

关于typescript - 如何使用 jsDoc 在自定义组件中记录此关键字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63270041/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com