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)
}

然后在使用时,会报如下的错误
image.png

是否要在注册全局组件时在声明文件中,写上组件的声明

阅读 3.6k
1 个回答
✓ 已被采纳

需要写组件声明的,可以看下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 {}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题