1. 环境准备
确保你已经安装了以下工具:
- Node.js(建议使用最新LTS版本)
- npm 或 yarn(包管理工具)
- Visual Studio Code(代码编辑器)
2. 创建Vue 3项目
使用Vue CLI创建一个新的Vue 3项目。
安装Vue CLI
如果你还没有安装Vue CLI,可以通过以下命令安装:
npm install -g @vue/cli | |
# 或者 | |
yarn global add @vue/cli |
创建项目
运行以下命令创建一个新的Vue 3项目:
vue create vue-element-admin
bash复制代码
在创建过程中,选择Vue 3版本,并根据需要选择其他配置(如Babel、Router、Vuex等)。
3. 安装Element Plus
Element Plus是Element UI的Vue 3版本。
安装Element Plus
进入项目目录,安装Element Plus:
cd vue-element-admin | |
npm install element-plus --save | |
# 或者 | |
yarn add element-plus |
bash复制代码
4. 配置项目布局
接下来,我们将配置项目的布局,包括顶部栏、侧边栏和主要区域。
创建布局组件
在src/components
目录下创建一个Layout.vue
文件,内容如下:
<template> | |
<el-container> | |
<el-header>顶部栏</el-header> | |
<el-container> | |
<el-aside width="200px">侧边栏</el-aside> | |
<el-main> | |
<router-view /> | |
</el-main> | |
</el-container> | |
</el-container> | |
</template> | |
<script> | |
export default { | |
name: 'Layout' | |
} | |
</script> | |
<style scoped> | |
.el-header { | |
background-color: #b3c0d1; | |
color: white; | |
line-height: 60px; | |
} | |
.el-aside { | |
background-color: #d3dce6; | |
color: #333; | |
line-height: 200px; | |
} | |
.el-main { | |
background-color: #e9eef3; | |
color: #333; | |
} | |
</style> |
bash复制代码
修改App.vue
将App.vue
修改为使用新的布局组件:
<template> | |
<Layout /> | |
</template> | |
<script> | |
import Layout from './components/Layout.vue' | |
export default { | |
name: 'App', | |
components: { | |
Layout | |
} | |
} | |
</script> | |
<style> | |
#app { | |
font-family: Avenir, Helvetica, Arial, sans-serif; | |
-webkit-font-smoothing: antialiased; | |
-moz-osx-font-smoothing: grayscale; | |
text-align: center; | |
color: #2c3e50; | |
} | |
</style> |
bash复制代码
5. 配置路由
确保你已经选择了Vue Router插件。
创建路由配置
在src/router
目录下创建index.js
文件,内容如下:
import { createRouter, createWebHistory } from 'vue-router' | |
import Home from '../views/Home.vue' | |
const routes = [ | |
{ | |
path: '/', | |
name: 'Home', | |
component: Home | |
}, | |
{ | |
path: '/about', | |
name: 'About', | |
component: () => import('../views/About.vue') | |
} | |
] | |
const router = createRouter({ | |
history: createWebHistory(process.env.BASE_URL), | |
routes | |
}) | |
export default router |
bash复制代码
修改main.js
确保在main.js
中使用了路由:
import { createApp } from 'vue' | |
import App from './App.vue' | |
import router from './router' | |
import ElementPlus from 'element-plus' | |
import 'element-plus/dist/index.css' | |
createApp(App).use(router).use(ElementPlus).mount('#app') |
bash复制代码
6. 创建视图组件
在src/views
目录下创建Home.vue
和About.vue
文件。
Home.vue
<template> | |
<div> | |
<h1>首页</h1> | |
</div> | |
</template> | |
<script> | |
export default { | |
name: 'Home' | |
} | |
</script> |
bash复制代码
About.vue
<template> | |
<div> | |
<h1>关于我们</h1> | |
</div> | |
</template> | |
<script> | |
export default { | |
name: 'About' | |
} | |
</script> |
bash复制代码
7. 运行项目
在项目根目录下运行以下命令启动项目:
npm run serve | |
# 或者 | |
yarn serve |
bash复制代码
8. 进一步完善
- 顶部栏和侧边栏内容:根据实际需求,完善顶部栏和侧边栏的内容,如添加导航菜单、用户信息等。
- 样式调整:根据设计稿调整页面样式。
- 功能开发