使用 unplugin-vue-components
外掛,它可以自動引入元件,並按需引入元件的樣式
1、安裝外掛
npm i unplugin-vue-components -D
2、vite.config.js
檔案中配置外掛
import { fileURLToPath, URL } from 'node:url' import { defineConfig } from 'vite' import vue from '@vitejs/plugin-vue' import Components from 'unplugin-vue-components/vite'; import { VantResolver } from 'unplugin-vue-components/resolvers'; export default defineConfig({ plugins: [ vue(), Components({ resolvers: [VantResolver()], }) ], resolve: { alias: { '@': fileURLToPath(new URL('./src', import.meta.url)) } } })