9
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

[vue3、typescript]Vueファイルのimportでエラー

Posted at

環境

vscode 1.82.2

vue 3.3.4
typescript 5.1.6
vite 4.4.9

手順

1..vueファイルをimport

main.ts
import App from '@/App.vue'

現象

以下のエラーが発生

モジュール '@/views/AboutView.vue' またはそれに対応する型宣言が見つかりません。

原因

.tsと.vueで拡張子が異なり、main.tsがApp.vueをTypescriptとして扱えないことが原因。

対策

main.tsと同階層に「shims.d.ts」(VueファイルをTypeScriptとして認識させるためのファイル)
を作成。

shims.d.ts
declare module "*.vue" {
  import type { DefineComponent } from "vue";
  const component: DefineComponent<{}, {}, any>;
  export default component;
}

参考

9
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?