0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

viteでimportのエイリアス設定

Posted at

viteでimportのエイリアスを設定

エイリアス設定

vite.cofig.ts
export default defineConfig({
	plugins: [react()],
	resolve: {
		alias: {
			'@': '/src',
			'@components': '/src/components',
		},
	},
});

エイリアスを使ってimportしてみる

import { Index } from '@components/index';

エイリアスでimportできるようになった。

問題発生

が、VsCode上では、モジュールが見つからないと言われた:frowning2:

スクリーンショット 2024-01-23 0.24.27.png

Cannot find module '@components/index' or its corresponding type declarations.

解決

tsconfig.jsonにも設定してあげないといけないらしい。

tsconfig.json
	"compilerOptions": {

		"baseUrl": ".",
		"paths": {
			"@/*": ["src/*"],
			"@components/*": ["src/components/*"]
		}
  }

解決!
スクリーンショット 2024-01-23 0.28.29.png

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?