搭建 Vue 3 + Vite 项目
pnpm create vite my-vue-admin --template vue
配置路径别名
import { resolve } from "path";
const pathSrc = resolve(__dirname, "src");
export default defineConfig(({ mode }) => {
return {
// 路径别名
resolve: {
alias: {
'@': pathSrc,
},
},
...
};
});
测试使用别名
import HelloWorld from './components/HelloWorld.vue'
import HelloWorld from '@/components/HelloWorld.vue'
按需自动导入API和组件
pnpm add -D unplugin-auto-import unplugin-vue-components
plugins: [
vue(),
// 自动导入API
AutoImport({
// 导入 Vue 的 API
imports: ['vue',],
}),
// 自动导入组件
Components({
// 指定自定义组件位置(默认:src/components)
dirs: [
'src/components/**',
'src/**/components',
],
}),
],
测试使用
<!-- src/components/HelloWorld.vue -->
// import { ref } from 'vue'
安装element-plus及自动导入
pnpm add element-plus
import { ElementPlusResolver } from "unplugin-vue-components/resolvers";
AutoImport({
...
resolvers: [
// 自动导入 Element Plus 相关函数,如:ElMessage, ElMessageBox... (带样式)
ElementPlusResolver(),
],
}),
// 自动导入组件
Components({
...
resolvers: [
// 自动导入 Element Plus 组件
ElementPlusResolver(),
],
}),
<!-- src/components/HelloWorld.vue -->
<div>
<el-button type="success">Success</el-button>
<el-button type="info">Info</el-button>
<el-button type="warning">Warning</el-button>
<el-button type="danger">Danger</el-button>
</div>
自动导入Icons图标
pnpm add -D unplugin-icons
plugins: [
AutoImport({
resolvers: [
// 自动导入图标组件
IconsResolver({}),
],
}),
// 自动导入组件
Components({
resolvers: [
// 自动注册图标组件
IconsResolver({
// element-plus图标库,其他图标库 https://icon-sets.iconify.design/
enabledCollections: ["ep"]
}),
],
}),
Icons({
// 自动安装图标库
autoInstall: true,
}),
],
<!-- src/components/HelloWorld.vue -->
<div>
<el-button type="success"><i-ep-SuccessFilled />Success</el-button>
<el-button type="info"><i-ep-InfoFilled />Info</el-button>
<el-button type="warning"><i-ep-WarningFilled />Warning</el-button>
<el-button type="danger"><i-ep-WarnTriangleFilled />Danger</el-button>
</div>
整合 SVG 图标
pnpm add -D vite-plugin-svg-icons
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
createSvgIconsPlugin({
// 指定需要缓存的图标文件夹
iconDirs: [path.resolve(process.cwd(), 'src/icons')],
// 指定symbolId格式
symbolId: 'icon-[dir]-[name]',
}),
参考: src\components\SvgIcon.vue
整合 SCSS
pnpm add -D sass
css: {
preprocessorOptions: {
scss: {
javascriptEnabled: true,
additionalData: `@use "@/assets/styles/global.scss" as *;`,
},
},
},
创建文件 "src/assets/style/global.scss"
$bg-color:
<style lang="scss" scoped>
.read-the-docs {
color:
background-color: $bg-color;
}
</style>
整合 UnoCSS
pnpm add -D unocss
// src/main.ts
import 'uno.css'
// vite.config.ts
import UnoCSS from 'unocss/vite'
export default {
plugins: [
UnoCSS({ /* options */ }),
],
}
<div class="text-red">Hello World!!</div>
整合 Pinia
pnpm add pinia
import { createPinia } from "pinia";
....use(createPinia())...
import { defineStore } from "pinia";
export const useCounterStore = defineStore("counter", () => {
// ref变量 → state 属性
const count = ref(0);
// computed计算属性 → getters
const double = computed(() => {
return count.value * 2;
});
// function函数 → actions
function increment() {
count.value++;
}
return { count, double, increment };
});
# <!-- src/App.vue -->
import { useCounterStore } from "@/store/counter";
const counterStore = useCounterStore();
...
<div>
<h1>父组件</h1>
<el-button @click="counterStore.increment">增加</el-button>
</div>
# <!-- src/components/HelloWorld.vue -->
<div>
<h1>子组件</h1>
<div>计数:{{ counterStore.count }}</div>
<div>双倍:{{ counterStore.double }}</div>
</div>
env 配置文件
VITE_APP_BASE_API=http://raiseinfo-admin.test
VITE_APP_WS_ENDPOINT=
VITE_APP_MQTT_ENDPOINT=ws://121.42.254.204:8083/mqtt
VITE_APP_BASE_API=http://raiseinfo-admin.test
VITE_APP_WS_ENDPOINT=
VITE_APP_MQTT_ENDPOINT=ws://121.42.254.204:8083/mqtt
server: {
host: '0.0.0.0',
port: env.VITE_PORT,
open: true,
proxy: {
'/admin': {
changeOrigin: true,
target: env.VITE_HOST,
rewrite: (path) => path.replace(new RegExp('^' + '/admin'), '/admin'),
},
'/upload': {
changeOrigin: true,
target: env.VITE_HOST,
rewrite: (path) =>
path.replace(new RegExp('^' + '/upload'), '/upload'),
},
},
},
整合 Axios
pnpm add axios