当前位置: 首页 > news >正文

Vue 3 + Composition API + Vite + Pinia + Element Plus 构建项目的完整指南

以下是使用 Vue 3 + Composition API + Vite + Pinia + Element Plus 构建项目的完整指南,包含 TypeScript 支持配置:


1. 创建项目

使用 Vite 初始化项目
npm create vite@latest my-vue-app -- --template vue-ts

如果不需要 TypeScript,使用 --template vue

进入项目目录
cd my-vue-app

2. 安装核心依赖

npm install vue-router@4 pinia element-plus @element-plus/icons-vue
npm install -D sass  # 如需使用 Element Plus 的 SCSS 变量

3. 项目结构

src/
├─ assets/           # 静态资源
├─ components/       # 公共组件
├─ views/            # 页面组件
├─ router/           # 路由配置
│  └─ index.ts
├─ stores/           # Pinia 状态管理
│  └─ counter.ts
├─ styles/           # 全局样式
│  └─ element.scss   # Element Plus 自定义主题
├─ App.vue
├─ main.ts

4. 配置 Element Plus

(1) 按需自动导入 (推荐)
npm install -D unplugin-vue-components unplugin-auto-import
(2) 修改 vite.config.ts
import { defineConfig } from 'vite'
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 defineConfig({
  plugins: [
    vue(),
    AutoImport({
      resolvers: [ElementPlusResolver()],
    }),
    Components({
      resolvers: [ElementPlusResolver()],
    }),
  ],
})
(3) 样式引入

main.ts 中添加:

import 'element-plus/dist/index.css'

5. 配置 Pinia

(1) 修改 main.ts
import { createApp } from 'vue'
import { createPinia } from 'pinia'
import App from './App.vue'

const app = createApp(App)
app.use(createPinia())
app.mount('#app')
(2) 创建示例 Store (stores/counter.ts)
import { defineStore } from 'pinia'

export const useCounterStore = defineStore('counter', {
  state: () => ({ count: 0 }),
  actions: {
    increment() {
      this.count++
    }
  }
})

6. 配置路由

(1) 创建 router/index.ts
import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'

const routes = [
  { path: '/', component: HomeView },
  { 
    path: '/about', 
    component: () => import('../views/AboutView.vue') 
  }
]

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes
})

export default router
(2) 修改 main.ts
import router from './router'

app.use(router)

7. 示例组件

views/HomeView.vue
<template>
  <div class="home">
    <el-button type="primary" @click="handleClick">
      Count is: {{ counter.count }}
    </el-button>
  </div>
</template>

<script setup lang="ts">
import { useCounterStore } from '@/stores/counter'
const counter = useCounterStore()

const handleClick = () => counter.increment()
</script>

8. 完整 App.vue 示例

<template>
  <div>
    <el-menu mode="horizontal" :router="true">
      <el-menu-item index="/">Home</el-menu-item>
      <el-menu-item index="/about">About</el-menu-item>
    </el-menu>

    <router-view />

    <el-footer style="text-align: center; margin-top: 20px;">
      © 2023 My Vue App
    </el-footer>
  </div>
</template>

<style>
#app {
  max-width: 1280px;
  margin: 0 auto;
  padding: 2rem;
}
</style>

9. 运行与构建

npm run dev    # 开发模式
npm run build  # 生产构建

可选配置

TypeScript 类型支持
  1. tsconfig.json 添加路径别名:
{
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "@/*": ["src/*"]
    }
  }
}
Element Plus 主题定制
  1. 创建 styles/element.scss
@forward "element-plus/theme-chalk/src/common/var.scss" with (
  $colors: (
    'primary': (
      'base': #409eff,
    )
  )
);
  1. 修改 vite.config.ts
css: {
  preprocessorOptions: {
    scss: {
      additionalData: `@use "@/styles/element.scss" as *;`
    }
  }
}

项目依赖清单

{
  "dependencies": {
    "vue": "^3.3.4",
    "vue-router": "^4.2.5",
    "pinia": "^2.1.7",
    "element-plus": "^2.4.1",
    "@element-plus/icons-vue": "^2.1.0"
  },
  "devDependencies": {
    "@vitejs/plugin-vue": "^4.5.0",
    "typescript": "^5.2.2",
    "unplugin-auto-import": "^0.17.4",
    "unplugin-vue-components": "^0.26.0",
    "sass": "^1.69.5",
    "vite": "^4.5.0"
  }
}

通过以上配置,您将获得一个现代化的 Vue 3 项目框架,具备:

  • 基于 Composition API 的开发模式
  • 高效的 Vite 构建工具
  • Pinia 状态管理
  • Element Plus UI 组件库
  • 完整的 TypeScript 支持
  • 自动化的组件/API 按需导入
  • 可扩展的主题定制能力

相关文章:

  • vue复习1~45
  • 数据安全与网络安全——问答复习
  • STL入门
  • SpringBoot3+Vue3开发公司库房管理系统
  • Cursor异常问题全解析-无限使用
  • 【ComfyUI】相似画绘制工作流教程
  • Ubuntu22云服务器添加2G Swap分区
  • C++中获取文件名的后缀
  • 深入探索 Python 中的 asyncio:异步编程的利器
  • es 3期 第27节-运用Script脚本实现复杂需求
  • 5500字,从零开始入门OpenCV的超基础操作~
  • Vue实现动态数据透视表(交叉表)
  • GCP(Google Cloud-native stack)的云原生技术栈介绍
  • Android之uCrop (裁剪) 的基本使用资料
  • 滑动窗口(1)—⻓度最⼩的⼦数组
  • 什么样的医疗器械进销存管理软件可以选择?
  • Channel Shuffle通道洗牌
  • ENSP学习day11
  • Angular由一个bug说起之十五:自定义基于Overlay的Tooltip
  • 未来AI视觉艺术,会替代人类设计师吗?
  • 雷神代刷推广网站/seo搜索优化公司排名
  • 网站栏目做ip地址访问限制/优秀的软文广告欣赏
  • 用源码网站好优化吗/seo标题优化导师咨询
  • wordpress swf插件/九江seo
  • 衡水做阿里巴巴网站/免费自助建站平台
  • 老房装修改造哪家好/seo关键词排名优化品牌