获取组件类型的方法
const AccountRef = ref<InstanceType<typeof LoginAccount>>()
typeof LoginAccount一直报红线提示错误
LoginAction: () => vo...' provides no match for the signature 'new (...args: any): any'.
使用的是webstorm codeing,通过webstorm自带右键新建vue文件,创建出来的文件是不带defineComponent的,所以导致上述的一直报红线
<script lang="ts">
export default {
}
</script>引用defineComponent
<script lang="ts">
import { defineComponent } from 'vue'
export default defineComponent({//注意这里的这个小括号不能漏
})
</script>使用 vue3 和 ts 时,为了获取 组件 ref 实例,就需要在 ref 函数的泛型中指定类型。
立即学习“前端免费学习笔记(深入)”;
vue 官方文档中 TypeScript 支持里已经告诉我们了一个获取组件类型的方法,InstanceType<typeof 组件名称>
使用方式如下:
const $userForm = ref<InstanceType<typeof userForm>>();
以上就是VUE3+TS获取组件类型遇到的坑怎么解决的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号