vue3使用全局组件时,ts报错找不到名称?
我新建了一个组件,hello.vue
<template>
<div>
123
</div>
</template
然后我在与 main.ts
的同级目录,新建了一个 index.ts
来写 install
方法
import hello from '@/components/hello.vue'
export const install = function (app: any) {
// 遍历注册全局组件
app.component('Hello', hello)
}
然后在使用时,会报如下的错误
是否要在注册全局组件时在声明文件中,写上组件的声明
回复
1个回答

test
2024-07-17
需要写组件声明的,可以看下Volar的文档,文档
// components.d.ts
declare module '@vue/runtime-core' { // Vue 3
// declare module 'vue' { // Vue 2.7
// declare module '@vue/runtime-dom' { // Vue <= 2.6.14
export interface GlobalComponents {
RouterLink: typeof import('vue-router')['RouterLink']
RouterView: typeof import('vue-router')['RouterView']
}
}
export {}
回复

适合作为回答的
- 经过验证的有效解决办法
- 自己的经验指引,对解决问题有帮助
- 遵循 Markdown 语法排版,代码语义正确
不该作为回答的
- 询问内容细节或回复楼层
- 与题目无关的内容
- “赞”“顶”“同问”“看手册”“解决了没”等毫无意义的内容