在使用ts的过程中发现,父组件调用子组件方法的时候
this.$refs.childThisParent .handleCommitInfo()
发现vscode报错Property 'handleCommitInfo' does not exist on type 'Vue的情况,但是实际上运行效果完全没问题。
主要原因是 vscode会根据声明文件自动进行类型推断的,这里没法知道childMethod的类型
因此,如下即可
(this.$refs.childThisParent as any).chlidMethod() 或者 (this.childThisParent as any).handleCommitInfo()
调用判断值也可以(this.childThisParent as any).canSubmit

本文探讨了在使用Vue.js时遇到的一个常见问题:即在父组件中调用子组件的方法时,VSCode出现类型推断错误提示,但实际上功能正常。文章提供了两种解决方法来规避IDE的类型检查警告。

692

被折叠的 条评论
为什么被折叠?



