This template should help get you started developing with Vue 3 in Vite.
VSCode + Volar (and disable Vetur).
TypeScript cannot handle type information for .vue
imports by default, so we replace the tsc
CLI with vue-tsc
for type checking. In editors, we need Volar to make the TypeScript language service aware of .vue
types.
See Vite Configuration Reference.
pnpm install
pnpm dev
pnpm build
Install element plus
pnpm install element-plus
Automatically import components
pnpm add unplugin-vue-components unplugin-auto-import -D
// vite.config.ts
import vue from '@vitejs/plugin-vue';
import AutoImport from 'unplugin-auto-import/vite';
import Components from 'unplugin-vue-components/vite';
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
export default {
plugins: [
vue(),
AutoImport({
resolvers: [ElementPlusResolver()],
}),
Components({
resolvers: [ElementPlusResolver()],
}),
],
};
Usage
<template>
<el-button>Default</el-button>
</template>
<script setup>
ElMessage.error('No need to import ElMessage');
</script>
Install vant
pnpm add vant
Automatically import components
pnpm add @vant/auto-import-resolver unplugin-vue-components unplugin-auto-import -D
// vite.config.ts
import vue from '@vitejs/plugin-vue';
import AutoImport from 'unplugin-auto-import/vite';
import Components from 'unplugin-vue-components/vite';
import { VantResolver } from '@vant/auto-import-resolver';
export default {
plugins: [
vue(),
AutoImport({
resolvers: [VantResolver()],
}),
Components({
resolvers: [VantResolver()],
}),
],
};
Viewport layout
pnpm add postcss-px-to-viewport-8-plugin -D
// vite.config.ts
import vue from "@vitejs/plugin-vue";
import postcsspxtoviewport8plugin from "postcss-px-to-viewport-8-plugin";
export default {
plugins: [vue()],
css: {
postcss: {
plugins: [
postcsspxtoviewport8plugin({
unitToConvert: "px",
viewportWidth: (file) => {
// Design draft width
let num = 750;
if (file.indexOf("vant") !== -1) {
// console.log(file);
num = 375;
}
return num;
},
}),
],
},
},
};
Usage
<template>
<van-button type="primary" />
</template>
<script setup>
showToast('No need to import showToast');
</script>