### Vue3 + Vite + TypeScript + Element Plus + ECharts 项目源码
以下是创建一个包含 Vue3、Vite、TypeScript、Element Plus 和 ECharts 的项目的基础指南和代码结构:
#### 初始化项目
通过 `npm` 或 `yarn` 创建一个新的 Vite 项目,并选择 Vue 和 TypeScript 支持:
```bash
npm create vite@latest my-vue-app --template vue-ts
cd my-vue-app
```
安装必要的依赖项,包括 Element Plus 和 ECharts:
```bash
npm install element-plus echarts
```
#### 配置环境
在 `vite.config.ts` 文件中引入并配置 Element Plus 和 ECharts。以下是一个完整的配置示例:
```typescript
import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';
import path from 'path';
// https://vitejs.dev/config/
export default defineConfig({
plugins: [vue()],
resolve: {
alias: {
'@': path.resolve(__dirname, './src'), // 路径别名配置
},
},
css: {
preprocessorOptions: {
scss: {
additionalData: `@use "@/styles/variables.scss" as *;`, // SCSS 变量全局导入
},
},
},
});
```
#### 安装 ESLint 和 Prettier 并配置
为了保持代码风格一致性和质量控制,可以按照以下方式设置 ESLint 和 Prettier。
1. **ESLint 插件安装**
```bash
npm install eslint eslint-plugin-vue @typescript-eslint/parser @typescript-eslint/eslint-plugin --save-dev
```
2. **Prettier 插件安装**
```bash
npm install prettier eslint-config-prettier eslint-plugin-prettier --save-dev
```
3. **配置 `.eslintrc.cjs`**
```javascript
module.exports = {
root: true,
env: {
node: true,
},
extends: [
'plugin:vue/vue3-essential',
'eslint:recommended',
'@vue/typescript/recommended',
'plugin:prettier/recommended', // 添加此行启用 Prettier
],
parserOptions: {
ecmaVersion: 2020,
},
rules: {
'no-console': process.env.NODE_ENV === 'production' ? 'warn' : 'off',
'no-debugger': process.env.NODE_ENV === 'production' ? 'warn' : 'off',
},
};
```
4. **配置 `.prettierrc`**
```json
{
"semi": false,
"singleQuote": true,
"tabWidth": 2,
"trailingComma": "all"
}
```
5. **更新 `package.json` 中的脚本命令**[^2]
```json
"scripts": {
"dev": "vite",
"build": "vite build",
"preview": "vite preview",
"lint": "eslint . --ext .vue,.ts,.tsx,.js,.jsx --fix",
"format": "prettier --write src/"
}
```
#### 使用 Element Plus
在 `main.ts` 文件中注册 Element Plus 组件库:
```typescript
import { createApp } from 'vue';
import App from './App.vue';
import ElementPlus from 'element-plus';
import 'element-plus/dist/index.css'; // 导入样式文件
const app = createApp(App);
app.use(ElementPlus);
app.mount('#app');
```
#### 使用 ECharts
在组件中动态加载 ECharts 图表实例:
```vue
<template>
<div ref="chartRef" style="width: 600px; height: 400px;"></div>
</template>
<script setup lang="ts">
import * as echarts from 'echarts';
import { onMounted, ref, Ref } from 'vue';
const chartRef: Ref<HTMLDivElement | null> = ref(null);
onMounted(() => {
const chartDom = chartRef.value;
if (chartDom) {
const myChart = echarts.init(chartDom);
const option = {
title: {
text: 'ECharts 示例',
},
tooltip: {},
legend: {
data: ['销量'],
},
xAxis: {
data: ['衬衫', '羊毛衫', '雪纺衫', '裤子', '高跟鞋', '袜子'],
},
yAxis: {},
series: [
{
name: '销量',
type: 'bar',
data: [5, 20, 36, 10, 10, 20],
},
],
};
myChart.setOption(option);
}
});
</script>
```
#### CSS 样式管理
可以通过 SCSS 进行全局样式定义,在 `src/styles/variables.scss` 文件中声明通用变量[^3]:
```scss
$primary-color: #409eff;
$font-size-base: 14px;
body {
font-family: Arial, sans-serif;
color: $primary-color;
}
```
---
###