搭建element-ui的Vue前端工程操作

博客转载而来,转载自:https://blog.csdn.net/davis2015csdn/article/details/72887591


一、安装npm镜像

(1)下载node.js, 配置node.js的环境变量

   检测PATH环境变量是否配置了Node.js,点击开始=》运行=》输入"cmd" => 输入命令"path"

 
   

检查Node.js版本

   在命令窗口输入:npm install -g cnpm –registry=https://registry.npm.taobao.org


二、安装全局vue-cli

(1)npm install -g vue-cli 回车,验证是否安装成功,在命令行中输入vue,出来vue的信息说明安装成功


三、全局安装 vue-cli

(1)npm install --global vue-cli 
四、创建一个基于 webpack 模板的新项目
(1)vue init webpack my-project (项目名)
(2)cd my-project
(3)npm install

(4)npm run dev


五、需要安装的环境
(1)npm install sass-loader --save-dev
(2)npm install gulp-sass
(3)npm install --save axios

(4)npm install element-ui -S

(5)npm install vuex --save

六、需要引入的包(element-ui)
 (1) import ElementUI from 'element-ui'
 (2) import 'element-ui/lib/theme-default/index.css'
 (3) import Vue from 'vue'

(4)使用:Vue.use(ElementUI)

六、项目代码结构

项目源码:https://github.com/davis0511/school-ui

(1)


(2)首页Home.vue


  1. & lt;template& gt;
  2. & lt;el-row class= "container"& gt;
  3. & lt;el-col :span= "24" class= "header"& gt;
  4. & lt;el-col :span= "20" class= "logo"& gt;
  5. & lt;img src= "./assets/logo4.png" /& gt; & lt;span& gt;学校管理& lt;i class= "txt"& gt;系统& lt; /i></span& gt;
  6. & lt; /el-col>
  7. <el-col :span="4" class="userinfo">
  8. <el-dropdown trigger="click">
  9. <span class="el-dropdown-link userinfo-inner"><img :src="this.sysUserAvatar" /& gt; { {sysUserName}}& lt; /span>
  10. <el-dropdown-menu slot="dropdown">
  11. <el-dropdown-item>我的消息</el-dropdown-item& gt;
  12. & lt;el-dropdown-item& gt;设置& lt; /el-dropdown-item>
  13. <el-dropdown-item divided @click.native="logout">退出登录</el-dropdown-item& gt;
  14. & lt; /el-dropdown-menu>
  15. </el-dropdown& gt;
  16. & lt; /el-col>
  17. </el-col& gt;
  18. & lt;el-col :span= "24" class= "main"& gt;
  19. & lt;aside& gt;
  20. & lt;el-menu :default-active= "$route.path" class= "el-menu-vertical-demo" @open= "handleopen" @close= "handleclose" @select= "handleselect"
  21. theme= "dark" unique-opened router& gt;
  22. & lt;template v- for= "(item,index) in $router.options.routes" v- if= "!item.hidden"& gt;
  23. & lt;el-submenu : index= "index+''" v- if= "!item.leaf"& gt;
  24. & lt;template slot= "title"& gt;& lt;i :class= "item.iconCls"& gt;& lt; /i>{{item.name}}</template& gt;
  25. & lt;el-menu-item v- for= "child in item.children" : index= "child.path" v- if= "!child.hidden"& gt;{{child.name}}& lt; /el-menu-item>
  26. </el-submenu& gt;
  27. & lt;el-menu-item v- if= "item.leaf&&item.children.length>0" : index= "item.children[0].path"& gt;& lt;i :class= "item.iconCls"& gt;& lt; /i>{{item.children[0].name}}</el-menu-item& gt;
  28. & lt; /template>
  29. </el-menu& gt;
  30. & lt; /aside>
  31. <section class="content-container">
  32. <div class="grid-content bg-purple-light">
  33. <el-col :span="24" class="breadcrumb-container">
  34. <strong class="title">{{$route.name}}</strong& gt;
  35. & lt;el-breadcrumb separator= "/" class= "breadcrumb-inner"& gt;
  36. & lt;el-breadcrumb-item v- for= "item in $route.matched"& gt;
  37. {{ item.name }}
  38. & lt; /el-breadcrumb-item>
  39. </el-breadcrumb& gt;
  40. & lt; /el-col>
  41. <el-col :span="24" class="content-wrapper">
  42. <transition>
  43. <router-view></router-view& gt;
  44. & lt; /transition>
  45. </el-col& gt;
  46. & lt; /div>
  47. </section& gt;
  48. & lt; /el-col>
  49. </el-row& gt;
  50. & lt; /template>
  51. <script>
  52. export default{
  53. data() {
  54. return {
  55. sysUserName:''
  56. }
  57. },
  58. methods:{
  59. onSubmit() {
  60. console.log('submit!');
  61. },
  62. handleopen() {
  63. / /console.log('handleopen');
  64. },
  65. handleclose() {
  66. / /console.log('handleclose');
  67. },
  68. handleselect: function (a, b) {
  69. },
  70. / /退出登录
  71. logout: function () {
  72. var _this = this;
  73. this.$confirm('确认退出吗?', '提示', {
  74. / /type: 'warning'
  75. }).then(() => {
  76. sessionStorage.removeItem('user');
  77. _this.$router.push('/login ');
  78. }).catch(() => {
  79. });
  80. }
  81. }
  82. }
  83. </script>
  84. <style scoped lang="scss">
  85. .container {
  86. position: absolute;
  87. top: 0px;
  88. bottom: 0px;
  89. width: 100%;
  90. .header {
  91. height: 60px;
  92. line-height: 60px;
  93. background: #1F2D3D;
  94. color: #c0ccda;
  95. .userinfo {
  96. text-align: right;
  97. padding-right: 35px;
  98. .userinfo-inner {
  99. color: #c0ccda;
  100. cursor: pointer;
  101. img {
  102. width: 40px;
  103. height: 40px;
  104. border-radius: 20px;
  105. margin: 10px 0px 10px 10px;
  106. float: right;
  107. }
  108. }
  109. }
  110. .logo {
  111. font-size: 22px;
  112. img {
  113. width: 40px;
  114. float: left;
  115. margin: 10px 10px 10px 18px;
  116. }
  117. .txt {
  118. color: #20a0ff
  119. }
  120. }
  121. }
  122. .main {
  123. background: #324057;
  124. position: absolute;
  125. top: 60px;
  126. bottom: 0px;
  127. overflow: hidden;
  128. aside {
  129. width: 230px;
  130. }
  131. .content-container {
  132. background: #f1f2f7;
  133. position: absolute;
  134. right: 0px;
  135. top: 0px;
  136. bottom: 0px;
  137. left: 230px;
  138. overflow-y: scroll;
  139. padding: 20px;
  140. .breadcrumb-container {
  141. margin-bottom: 15px;
  142. .title {
  143. width: 200px;
  144. float: left;
  145. color: #475669;
  146. }
  147. .breadcrumb-inner {
  148. float: right;
  149. }
  150. }
  151. .content-wrapper {
  152. background-color: #fff;
  153. box-sizing: border-box;
  154. }
  155. }
  156. }
  157. }
  158. </style>




(3)App.vue

(4)main.js

[html]  view plain  copy
  1. <code class="language-html">import Vue from 'vue'  
  2. import Router from 'vue-router'   
  3. import App from './App'  
  4. import routes from './router'  
  5. import ElementUI from 'element-ui'  
  6. import 'element-ui/lib/theme-default/index.css'  
  7.   
  8.   
  9. Vue.use(Router)    
  10. Vue.use(ElementUI)  
  11. const router = new Router({  
  12.   routes  
  13. });  
  14.   
  15.   
  16. Vue.config.productionTip = false  
  17.   
  18.   
  19. new Vue({   
  20.   router,    
  21.   render: h => h(App)  
  22. }).$mount('#app')</code>  


(5)router.js



  1. import Home from './Home'
  2. import classes from './class/classes'
  3. import student from './student/student'
  4. let router = [
  5. {
  6. path: '/',
  7. name: '学校',
  8. component: Home,
  9. redirect: '/classes',
  10. iconCls: 'fa fa-id-card-o',
  11. children: [
  12. { path: '/classes', component: classes, name: '班级管理' },
  13. { path: '/student', component: student, name: '学生管理' }
  14. ]
  15. }
  16. ];
  17. export default router;


七、完成之后,npm run dev; 界面渲染如下:


版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/davis2015csdn/article/details/72887591

一、安装npm镜像

(1)下载node.js, 配置node.js的环境变量

   检测PATH环境变量是否配置了Node.js,点击开始=》运行=》输入"cmd" => 输入命令"path"

 
 

检查Node.js版本

   在命令窗口输入:npm install -g cnpm –registry=https://registry.npm.taobao.org


二、安装全局vue-cli

(1)npm install -g vue-cli 回车,验证是否安装成功,在命令行中输入vue,出来vue的信息说明安装成功


三、全局安装 vue-cli

(1)npm install --global vue-cli 
四、创建一个基于 webpack 模板的新项目
(1)vue init webpack my-project (项目名)
(2)cd my-project
(3)npm install

(4)npm run dev


五、需要安装的环境
(1)npm install sass-loader --save-dev
(2)npm install gulp-sass
(3)npm install --save axios

(4)npm install element-ui -S

(5)npm install vuex --save

六、需要引入的包(element-ui)
 (1) import ElementUI from 'element-ui'
 (2) import 'element-ui/lib/theme-default/index.css'
 (3) import Vue from 'vue'

(4)使用:Vue.use(ElementUI)

六、项目代码结构

项目源码:https://github.com/davis0511/school-ui

(1)


(2)首页Home.vue


  1. & lt;template& gt;
  2. & lt;el-row class= "container"& gt;
  3. & lt;el-col :span= "24" class= "header"& gt;
  4. & lt;el-col :span= "20" class= "logo"& gt;
  5. & lt;img src= "./assets/logo4.png" /& gt; & lt;span& gt;学校管理& lt;i class= "txt"& gt;系统& lt; /i&gt;&lt;/span& gt;
  6. & lt; /el-col&gt;
  7. &lt;el-col :span="4" class="userinfo"&gt;
  8. &lt;el-dropdown trigger="click"&gt;
  9. &lt;span class="el-dropdown-link userinfo-inner"&gt;&lt;img :src="this.sysUserAvatar" /& gt; { {sysUserName}}& lt; /span&gt;
  10. &lt;el-dropdown-menu slot="dropdown"&gt;
  11. &lt;el-dropdown-item&gt;我的消息&lt;/el-dropdown-item& gt;
  12. & lt;el-dropdown-item& gt;设置& lt; /el-dropdown-item&gt;
  13. &lt;el-dropdown-item divided @click.native="logout"&gt;退出登录&lt;/el-dropdown-item& gt;
  14. & lt; /el-dropdown-menu&gt;
  15. &lt;/el-dropdown& gt;
  16. & lt; /el-col&gt;
  17. &lt;/el-col& gt;
  18. & lt;el-col :span= "24" class= "main"& gt;
  19. & lt;aside& gt;
  20. & lt;el-menu :default-active= "$route.path" class= "el-menu-vertical-demo" @open= "handleopen" @close= "handleclose" @select= "handleselect"
  21. theme= "dark" unique-opened router& gt;
  22. & lt;template v- for= "(item,index) in $router.options.routes" v- if= "!item.hidden"& gt;
  23. & lt;el-submenu : index= "index+''" v- if= "!item.leaf"& gt;
  24. & lt;template slot= "title"& gt;& lt;i :class= "item.iconCls"& gt;& lt; /i&gt;{{item.name}}&lt;/template& gt;
  25. & lt;el-menu-item v- for= "child in item.children" : index= "child.path" v- if= "!child.hidden"& gt;{{child.name}}& lt; /el-menu-item&gt;
  26. &lt;/el-submenu& gt;
  27. & lt;el-menu-item v- if= "item.leaf&amp;&amp;item.children.length&gt;0" : index= "item.children[0].path"& gt;& lt;i :class= "item.iconCls"& gt;& lt; /i&gt;{{item.children[0].name}}&lt;/el-menu-item& gt;
  28. & lt; /template&gt;
  29. &lt;/el-menu& gt;
  30. & lt; /aside&gt;
  31. &lt;section class="content-container"&gt;
  32. &lt;div class="grid-content bg-purple-light"&gt;
  33. &lt;el-col :span="24" class="breadcrumb-container"&gt;
  34. &lt;strong class="title"&gt;{{$route.name}}&lt;/strong& gt;
  35. & lt;el-breadcrumb separator= "/" class= "breadcrumb-inner"& gt;
  36. & lt;el-breadcrumb-item v- for= "item in $route.matched"& gt;
  37. {{ item.name }}
  38. & lt; /el-breadcrumb-item&gt;
  39. &lt;/el-breadcrumb& gt;
  40. & lt; /el-col&gt;
  41. &lt;el-col :span="24" class="content-wrapper"&gt;
  42. &lt;transition&gt;
  43. &lt;router-view&gt;&lt;/router-view& gt;
  44. & lt; /transition&gt;
  45. &lt;/el-col& gt;
  46. & lt; /div&gt;
  47. &lt;/section& gt;
  48. & lt; /el-col&gt;
  49. &lt;/el-row& gt;
  50. & lt; /template&gt;
  51. &lt;script&gt;
  52. export default{
  53. data() {
  54. return {
  55. sysUserName:''
  56. }
  57. },
  58. methods:{
  59. onSubmit() {
  60. console.log('submit!');
  61. },
  62. handleopen() {
  63. / /console.log('handleopen');
  64. },
  65. handleclose() {
  66. / /console.log('handleclose');
  67. },
  68. handleselect: function (a, b) {
  69. },
  70. / /退出登录
  71. logout: function () {
  72. var _this = this;
  73. this.$confirm('确认退出吗?', '提示', {
  74. / /type: 'warning'
  75. }).then(() =&gt; {
  76. sessionStorage.removeItem('user');
  77. _this.$router.push('/login ');
  78. }).catch(() =&gt; {
  79. });
  80. }
  81. }
  82. }
  83. &lt;/script&gt;
  84. &lt;style scoped lang="scss"&gt;
  85. .container {
  86. position: absolute;
  87. top: 0px;
  88. bottom: 0px;
  89. width: 100%;
  90. .header {
  91. height: 60px;
  92. line-height: 60px;
  93. background: #1F2D3D;
  94. color: #c0ccda;
  95. .userinfo {
  96. text-align: right;
  97. padding-right: 35px;
  98. .userinfo-inner {
  99. color: #c0ccda;
  100. cursor: pointer;
  101. img {
  102. width: 40px;
  103. height: 40px;
  104. border-radius: 20px;
  105. margin: 10px 0px 10px 10px;
  106. float: right;
  107. }
  108. }
  109. }
  110. .logo {
  111. font-size: 22px;
  112. img {
  113. width: 40px;
  114. float: left;
  115. margin: 10px 10px 10px 18px;
  116. }
  117. .txt {
  118. color: #20a0ff
  119. }
  120. }
  121. }
  122. .main {
  123. background: #324057;
  124. position: absolute;
  125. top: 60px;
  126. bottom: 0px;
  127. overflow: hidden;
  128. aside {
  129. width: 230px;
  130. }
  131. .content-container {
  132. background: #f1f2f7;
  133. position: absolute;
  134. right: 0px;
  135. top: 0px;
  136. bottom: 0px;
  137. left: 230px;
  138. overflow-y: scroll;
  139. padding: 20px;
  140. .breadcrumb-container {
  141. margin-bottom: 15px;
  142. .title {
  143. width: 200px;
  144. float: left;
  145. color: #475669;
  146. }
  147. .breadcrumb-inner {
  148. float: right;
  149. }
  150. }
  151. .content-wrapper {
  152. background-color: #fff;
  153. box-sizing: border-box;
  154. }
  155. }
  156. }
  157. }
  158. &lt;/style&gt;




(3)App.vue

(4)main.js

[html]  view plain  copy
  1. <code class="language-html">import Vue from 'vue'  
  2. import Router from 'vue-router'   
  3. import App from './App'  
  4. import routes from './router'  
  5. import ElementUI from 'element-ui'  
  6. import 'element-ui/lib/theme-default/index.css'  
  7.   
  8.   
  9. Vue.use(Router)    
  10. Vue.use(ElementUI)  
  11. const router = new Router({  
  12.   routes  
  13. });  
  14.   
  15.   
  16. Vue.config.productionTip = false  
  17.   
  18.   
  19. new Vue({   
  20.   router,    
  21.   render: h => h(App)  
  22. }).$mount('#app')</code>  


(5)router.js



  1. import Home from './Home'
  2. import classes from './class/classes'
  3. import student from './student/student'
  4. let router = [
  5. {
  6. path: '/',
  7. name: '学校',
  8. component: Home,
  9. redirect: '/classes',
  10. iconCls: 'fa fa-id-card-o',
  11. children: [
  12. { path: '/classes', component: classes, name: '班级管理' },
  13. { path: '/student', component: student, name: '学生管理' }
  14. ]
  15. }
  16. ];
  17. export default router;


七、完成之后,npm run dev; 界面渲染如下:


猜你喜欢

转载自blog.csdn.net/meng19910117/article/details/80961434