当前位置:   article > 正文

Vue2+element-ui后台管理系统(静态页面)_vue+elementui管理项目界面

vue+elementui管理项目界面

项目所需

node:https://nodejs.org/en/

git:https://git-scm.com/

vue:https://v2.cn.vuejs.org/v2/guide/installation.html

element-ui:https://element.eleme.cn/#/zh-CN/component/installation

项目所需:https://pan.baidu.com/s/1ua0jp9YCtPH6slE49HDUBw

提取码:kkkk

新建项目

在node和vue都调试、配置好的情况下使用vscode 在终端中输入命令

npm i -g @vue/cli 安装vue脚手架

安装成功后通过vue --version 可以查看版本

vue create 项目名 新建项目

vue create control

空格为取消选项 回车为选中

根据个人习惯选择css样式 本人喜欢使用sass

一路回车等待创建

此界面为创建成功

cd control 来到项目的目录 进行启动 (git命令自行学习)

此界面为开启成功

项目初始化

将以下标记全部删除

router/index.js最终页面

将views中文件全部删除,并新建Login.vue 和 Register.vue 文件,修改App.vue

Login 、 Register 和 App.vue 文件代码

  1. // Login、Register和App.vue 文件代码
  2. <template>
  3. <div>
  4. Login // Register
  5. </div>
  6. </template>
  7. <script>
  8. export default {}
  9. </script>
  10. <style lang="scss" scoped>
  11. </style>

在router/index.js中配置路由信息

在App.vue中添加路由出口 <router-view></router-view>

此时打开在浏览器打开项目 为此界面即为成功

页面部署

引入element-ui

element-ui 哪里需要引哪里

代码

router/index.js

  1. //router/index.js
  2. import Vue from "vue";
  3. import VueRouter from "vue-router";
  4. Vue.use(VueRouter);
  5. const router = new VueRouter({
  6. mode: "history",
  7. base: process.env.BASE_URL,
  8. routes:[
  9. {path:'/login',component:()=>import('../views/Login.vue')},
  10. {
  11. path:'/home',
  12. component:()=>import('../views/Home.vue'),
  13. children:[
  14. {path:'/home/one-one',component:()=>import('../views/OneOne.vue')},
  15. {path:'/home/one-two',component:()=>import('../views/OneTwo.vue')},
  16. {path:'/home/one-thr',component:()=>import('../views/OneThr.vue')},
  17. {path:'/home/',redirect:'/home/one-one'}
  18. ]
  19. },
  20. {path:'/register',component:()=>import('../views/Register.vue')},
  21. {path:'/',redirect:'/login'}
  22. ]
  23. });
  24. export default router;

main.js

  1. import Vue from 'vue'
  2. import App from './App.vue'
  3. import router from './router'
  4. import store from './store'
  5. import ElementUI from 'element-ui'
  6. import 'element-ui/lib/theme-chalk/index.css';
  7. Vue.config.productionTip = false
  8. Vue.use(ElementUI);
  9. new Vue({
  10. router,
  11. store,
  12. el: '#app',
  13. render: h => h(App)
  14. }).$mount('#app')

Home.vue

  1. <template>
  2. <div class="control">
  3. <div class="tit">
  4. <div class="userName">
  5. <h4>{{ $store.state.username }}</h4>
  6. <span>欢迎您</span>
  7. </div>
  8. <div class="userInfo">
  9. <div class="userImg" @click="changeImg" @mouseover="close">
  10. <img src="" alt="">
  11. </div>
  12. </div>
  13. </div>
  14. <div class="banner">
  15. <div class="left">
  16. <div class="menu">
  17. <el-radio-group v-model="isCollapse" style="margin-bottom: 20px;">
  18. <el-radio-button :label="false">展开</el-radio-button>
  19. <el-radio-button :label="true">收起</el-radio-button>
  20. </el-radio-group>
  21. <el-menu default-active="1-4-1" class="el-menu-vertical-demo" @open="handleOpen" @close="handleClose"
  22. :collapse="isCollapse">
  23. <el-submenu index="1">
  24. <template slot="title">
  25. <i class="el-icon-location"></i>
  26. <span slot="title">导航一</span>
  27. </template>
  28. <el-menu-item-group>
  29. <span slot="title">分组一</span>
  30. <el-menu-item index="1-1" @click="option(11)">选项1</el-menu-item>
  31. <el-menu-item index="1-2" @click="option(12)">选项2</el-menu-item>
  32. </el-menu-item-group>
  33. <el-menu-item-group title="分组2">
  34. <el-menu-item index="1-3" @click="option(13)">选项3</el-menu-item>
  35. </el-menu-item-group>
  36. <el-submenu index="1-4">
  37. <span slot="title">选项4</span>
  38. <el-menu-item index="1-4-1">选项1</el-menu-item>
  39. </el-submenu>
  40. </el-submenu>
  41. <el-menu-item index="2">
  42. <i class="el-icon-menu"></i>
  43. <span slot="title">导航二</span>
  44. </el-menu-item>
  45. <el-menu-item index="3" disabled>
  46. <i class="el-icon-document"></i>
  47. <span slot="title">导航三</span>
  48. </el-menu-item>
  49. <el-menu-item index="4">
  50. <i class="el-icon-setting"></i>
  51. <span slot="title">导航四</span>
  52. </el-menu-item>
  53. </el-menu>
  54. </div>
  55. </div>
  56. <div class="right">
  57. <router-view></router-view>
  58. </div>
  59. </div>
  60. <div class="ImgMenu" v-if="show" @mouseover="changeImg('self')" @mouseout="close">
  61. <el-upload class="avatar-uploader" action="https://jsonplaceholder.typicode.com/posts/" :show-file-list="false"
  62. :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload">
  63. <img v-if="imageUrl" :src="imageUrl" class="avatar">
  64. <i v-else class="el-icon-plus avatar-uploader-icon"></i>
  65. </el-upload>
  66. <div class="switch">
  67. <ul class="conList">
  68. <li @click="changeUser">
  69. 切换账号
  70. </li>
  71. </ul>
  72. </div>
  73. </div>
  74. </div>
  75. </template>
  76. <script>
  77. export default {
  78. data() {
  79. return {
  80. isCollapse: true,
  81. show: false,
  82. imageUrl: '',
  83. t: null
  84. };
  85. },
  86. methods: {
  87. changeImg(key) {
  88. if (key == 'self') {
  89. this.show = true
  90. clearTimeout(this.t)
  91. } else {
  92. this.show = this.show ? false : true
  93. }
  94. },
  95. close() {
  96. let that = this
  97. this.t = setTimeout(() => {
  98. that.show = false
  99. }, 3000);
  100. },
  101. changeUser() {
  102. this.$router.push('/login')
  103. },
  104. handleOpen(key, keyPath) {
  105. console.log(key, keyPath);
  106. },
  107. handleClose(key, keyPath) {
  108. console.log(key, keyPath);
  109. },
  110. handleAvatarSuccess(res, file) {
  111. this.imageUrl = URL.createObjectURL(file.raw);
  112. },
  113. beforeAvatarUpload(file) {
  114. const isJPG = file.type === 'image/jpeg';
  115. const isLt2M = file.size / 1024 / 1024 < 2;
  116. if (!isJPG) {
  117. this.$message.error('上传头像图片只能是 JPG 格式!');
  118. }
  119. if (!isLt2M) {
  120. this.$message.error('上传头像图片大小不能超过 2MB!');
  121. }
  122. return isJPG && isLt2M;
  123. },
  124. // 切换选项
  125. option(key) {
  126. switch (key) {
  127. case 11:
  128. this.$router.push('/home/one-one')
  129. break;
  130. case 12:
  131. this.$router.push('/home/one-two')
  132. break;
  133. case 13:
  134. this.$router.push('/home/one-thr')
  135. break;
  136. }
  137. }
  138. },
  139. mounted() {
  140. this.$store.state.username = localStorage.getItem('username')
  141. },
  142. beforeRouteLeave(to, from, next) {
  143. localStorage.clear()
  144. next()
  145. }
  146. }
  147. </script>
  148. <style lang="scss" scoped>
  149. ul {
  150. list-style: none;
  151. }
  152. .control {
  153. width: 100%;
  154. height: 100%;
  155. display: flex;
  156. flex-direction: column;
  157. justify-content: space-between;
  158. }
  159. .conList {
  160. display: flex;
  161. flex-direction: column;
  162. justify-content: center;
  163. align-items: center;
  164. }
  165. .tit {
  166. width: 100%;
  167. height: 50px;
  168. background-color: rgb(27, 101, 237);
  169. display: flex;
  170. justify-content: right;
  171. .userInfo {
  172. width: 50px;
  173. height: 50px;
  174. margin-right: 30px;
  175. .userImg {
  176. width: 100%;
  177. height: 100%;
  178. border-radius: 50%;
  179. background-color: rgb(11, 144, 100);
  180. }
  181. }
  182. }
  183. .el-menu-vertical-demo:not(.el-menu--collapse) {
  184. width: 200px;
  185. min-height: 400px;
  186. }
  187. .avatar-uploader .el-upload {
  188. border: 1px dashed #d9d9d9;
  189. border-radius: 6px;
  190. cursor: pointer;
  191. position: relative;
  192. overflow: hidden;
  193. }
  194. .avatar-uploader .el-upload:hover {
  195. border-color: #409EFF;
  196. }
  197. .avatar-uploader-icon {
  198. font-size: 28px;
  199. color: #8c939d;
  200. width: 178px;
  201. height: 178px;
  202. line-height: 178px;
  203. text-align: center;
  204. }
  205. .avatar {
  206. width: 178px;
  207. height: 178px;
  208. display: block;
  209. }
  210. .ImgMenu {
  211. width: 200px;
  212. height: 400px;
  213. border-radius: 10px;
  214. position: absolute;
  215. right: 30px;
  216. top: 70px;
  217. background-color: rgba(155, 150, 150, .3);
  218. }
  219. .userName {
  220. width: 200px;
  221. height: 50px;
  222. display: flex;
  223. justify-content: space-around;
  224. align-items: center;
  225. }
  226. .banner {
  227. width: 100%;
  228. display: flex;
  229. justify-content: space-between;
  230. flex: 1;
  231. }
  232. .left {
  233. width: 15%;
  234. height: 100%;
  235. }
  236. .right {
  237. width: 85%;
  238. height: 100%;
  239. }
  240. </style>

Login.vue

  1. <template>
  2. <div class="login">
  3. <el-form :model="admin" ref="login" :rules="rules" @keyup.enter.native="login">
  4. <el-form-item prop="username">
  5. <el-input placeholder="请输入用户名" v-model="admin.username"></el-input>
  6. </el-form-item>
  7. <el-form-item prop="password">
  8. <el-input placeholder="请输入密码" type="password" v-model="admin.password"></el-input>
  9. </el-form-item>
  10. <el-button @click="login">
  11. 登录
  12. </el-button>
  13. <el-button>
  14. <router-link to="/register">注册</router-link>
  15. </el-button>
  16. </el-form>
  17. </div>
  18. </template>
  19. <script>
  20. export default {
  21. data() {
  22. return {
  23. admin: {
  24. username: "",
  25. password: ""
  26. },
  27. rules: {
  28. username: [
  29. { required: true, message: "用户名格式错误", trigger: "blur" },
  30. { min: 3, max: 15, message: "长度在 3 到 15 个字符", trigger: "blur" }
  31. ],
  32. password: [
  33. { required: true, message: "密码格式错误", trigger: "blur" },
  34. { min: 3, max: 15, message: "长度在 3 到 15 个字符", trigger: "blur" }
  35. ]
  36. }
  37. };
  38. },
  39. methods: {
  40. login() {
  41. let that = this
  42. this.$refs.login.validate((valid) => {
  43. if (valid) {
  44. that.$router.push('/home')
  45. }
  46. else {
  47. console.log("error submit!!");
  48. return false;
  49. }
  50. });
  51. }
  52. },
  53. beforeRouteLeave(to, from, next) {
  54. this.$store.state.username = this.admin.username
  55. localStorage.setItem('username', this.admin.username)
  56. next()
  57. }
  58. }
  59. </script>
  60. <style lang="scss" scoped>
  61. .login {
  62. width: 620px;
  63. height: 349px;
  64. position: absolute;
  65. top: 50%;
  66. left: 50%;
  67. transform: translate(-50%, -50%);
  68. padding: 20px 30px;
  69. border-radius: 20px;
  70. box-shadow: 5px 5px 20px rgb(39, 38, 38);
  71. background-image: linear-gradient(skyblue,orange);
  72. }
  73. </style>

OneOne.vue

  1. <template>
  2. <div class="one-two">
  3. <h2>1-1</h2>
  4. <ul class="dataList">
  5. <li v-for="item in oneData">
  6. {{ item }}
  7. </li>
  8. </ul>
  9. </div>
  10. </template>
  11. <script>
  12. export default {
  13. data(){
  14. return{
  15. oneData:['111','222','333','444']
  16. }
  17. }
  18. }
  19. </script>
  20. <style lang="scss" scoped>
  21. *{
  22. margin: 0;
  23. padding: 0;
  24. }
  25. ul{
  26. list-style: none;
  27. }
  28. .one-two{
  29. width: 100%;
  30. height: 100%;
  31. }
  32. .dataList{
  33. display: flex;
  34. justify-content: space-around;
  35. flex-wrap: wrap;
  36. }
  37. .dataList li{
  38. width: 533px;
  39. height: 300px;
  40. img{
  41. width: 100%;
  42. }
  43. }
  44. </style>

自己动手丰衣足食!!!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/113324
推荐阅读
相关标签
  

闽ICP备14008679号