1. 引言
随着互联网技术的飞速发展,企业级后台系统的需求日益增长。Vue Element Admin 框架凭借其易用性、高性能和丰富的组件库,成为了许多开发者的首选。本文将带你轻松入门 Vue Element Admin 框架,并快速搭建一个企业级后台系统。
2. 环境搭建
2.1 安装 Node.js
首先,确保你的电脑上安装了 Node.js。Vue Element Admin 框架需要 Node.js 环境,你可以从官网下载并安装。
2.2 安装 Vue CLI
Vue CLI 是一个基于 Vue.js 的开发工具,用于快速搭建 Vue.js 项目。打开命令行工具,执行以下命令安装 Vue CLI:
npm install -g @vue/cli
2.3 创建项目
安装完成后,创建一个新的 Vue.js 项目:
vue create vue-element-admin
选择默认配置或手动配置,然后进入项目目录:
cd vue-element-admin
3. 安装 Vue Element Admin
在项目目录中,安装 Vue Element Admin:
npm install vue-element-admin --save
4. 配置项目
4.1 引入 Element UI
在 src/main.js 文件中,引入 Element UI:
import Vue from 'vue'
import Element from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import App from './App.vue'
Vue.use(Element)
new Vue({
el: '#app',
render: h => h(App)
})
4.2 配置路由
在 src/router/index.js 文件中,配置路由:
import Vue from 'vue'
import Router from 'vue-router'
import Layout from '@/layout'
Vue.use(Router)
const router = new Router({
scrollBehavior: () => ({ y: 0 }),
routes: [
{
path: '/',
component: Layout,
children: [
{
path: '',
name: 'Dashboard',
component: () => import('@/views/dashboard/index')
}
]
}
]
})
export default router
4.3 配置权限
在 src/store/modules/permission.js 文件中,配置权限:
const state = {
routes: []
}
const mutations = {
SET_ROUTES(state, routes) {
state.routes = routes
}
}
const actions = {
generateRoutes({ commit }, roles) {
const accessedRoutes = filterAsyncRoutes(asyncRoutes, roles)
commit('SET_ROUTES', accessedRoutes)
return accessedRoutes
}
}
export default {
namespaced: true,
state,
mutations,
actions
}
5. 搭建后台系统
5.1 创建页面
在 src/views 目录下,创建一个名为 login 的文件夹,并在其中创建 index.vue 文件:
<template>
<div class="login-container">
<el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
<el-form-item prop="username">
<el-input v-model="loginForm.username" auto-complete="off" placeholder="用户名"></el-input>
</el-form-item>
<el-form-item prop="password">
<el-input type="password" v-model="loginForm.password" auto-complete="off" placeholder="密码"></el-input>
</el-form-item>
<el-form-item>
<el-button type="primary" @click="handleLogin">登录</el-button>
</el-form-item>
</el-form>
</div>
</template>
<script>
export default {
data() {
return {
loginForm: {
username: 'admin',
password: '123456'
},
loginRules: {
username: [
{ required: true, trigger: 'blur', message: '请输入用户名' }
],
password: [
{ required: true, trigger: 'blur', message: '请输入密码' }
]
}
}
},
methods: {
handleLogin() {
this.$refs.loginForm.validate((valid) => {
if (valid) {
this.$router.push({ path: '/dashboard' })
} else {
console.log('error submit!!')
return false
}
})
}
}
}
</script>
<style lang="scss" scoped>
.login-container {
min-height: 100vh;
width: 100%;
background-color: #2d3a4b;
overflow: hidden;
.login-form {
position: relative;
width: 520px;
max-width: 100%;
padding: 160px 35px 0;
margin: 0 auto;
overflow: hidden;
}
}
</style>
5.2 配置路由权限
在 src/router/index.js 文件中,添加登录路由:
{
path: '/login',
name: 'Login',
component: () => import('@/views/login/index')
}
在 src/store/modules/permission.js 文件中,添加登录路由权限:
const asyncRoutes = [
{
path: '/login',
component: () => import('@/views/login/index'),
hidden: true
}
]
5.3 登录功能
在 src/views/login/index.vue 文件中,实现登录功能:
export default {
data() {
return {
loginForm: {
username: 'admin',
password: '123456'
},
loginRules: {
username: [
{ required: true, trigger: 'blur', message: '请输入用户名' }
],
password: [
{ required: true, trigger: 'blur', message: '请输入密码' }
]
}
}
},
methods: {
handleLogin() {
this.$refs.loginForm.validate((valid) => {
if (valid) {
this.$store.dispatch('user/login', this.loginForm).then(() => {
this.$router.push({ path: '/dashboard' })
}).catch(() => {
this.$message.error('登录失败,请稍后再试')
})
} else {
console.log('error submit!!')
return false
}
})
}
}
}
在 src/store/modules/user.js 文件中,实现登录功能:
const state = {
token: ''
}
const mutations = {
SET_TOKEN(state, token) {
state.token = token
}
}
const actions = {
login({ commit }, userInfo) {
return new Promise((resolve, reject) => {
// 登录逻辑...
commit('SET_TOKEN', token)
resolve()
})
}
}
export default {
namespaced: true,
state,
mutations,
actions
}
6. 总结
通过本文的介绍,相信你已经掌握了 Vue Element Admin 框架的入门知识,并能够快速搭建一个企业级后台系统。在实际开发过程中,你可以根据自己的需求,不断完善和优化项目。祝你在 Vue Element Admin 框架的世界里,畅游无阻!
