定制化开发猿创征文|如何使用 Element UI? 以登录框为例带你感受一下基础使用

目录

 


前言

,定制化开发一套为开发者、定制化开发设计师和产品经理准备的基于 Vue 2.0 的组件库,定制化开发提供了配套设计资源,定制化开发帮助你的网站快速成型。定制化开发由饿了么公司前端团队开源。

一、安装(所有内容)

npm i element-ui -S   终端安装

在main.js中引入,定制化开发此方法引入了所有功能,定制化开发会导致项目文件较大,推荐按需引入,需要什么引入什么

  1. import ElementUI from 'element-ui';
  2. import 'element-ui/lib/theme-chalk/index.css';
  3. Vue.use(ElementUI);

二、按需引入

npm i element-ui -S 终端安装

npm install babel-plugin-component -D  安装 开发依赖

修改babel.config.js

  1. module.exports = {
  2.   presets: [
  3.     '@vue/app',
  4. ["@babel/preset-env", { "modules": false }]
  5.   ],
  6.    plugins: [
  7.       [
  8.         "component",
  9.         {
  10.           "libraryName": "element-ui",
  11.           "styleLibraryName": "theme-chalk"
  12.         }
  13.       ]
  14.     ]
  15. }

新创一个专属的引入文件夹与src同级,引入想要引入的内容

  1. import Vue from 'vue'
  2. import {Button,Form,FormItem,Input,Col,Row,Message} from 'element-ui';
  3. Vue.use(Button)
  4. Vue.use(Form)
  5. Vue.use(FormItem)
  6. Vue.use(Input)
  7. Vue.use(Col)
  8. Vue.use(Row)
  9. // 弹出框挂载到vue原型身上,这样每个组件都是用this.message
  10. Vue.prototype.$message = Message

main.js引入文件夹路径,这里拿我的为例

import '@/plugins/element'

三、案例演示

1.案前整理

提前准备了图片,样式,来配合实现一个较为美观的登录框,如果需要源码样式,私信留下邮箱免费领取,看见就回

这里我还使用了简单的路由效果,来展现跳转页面,下面展示基础的效果

2.代码演示(后附源码) 

首先我们需要创建一个路由组件,将内容样式导入,这里直接跳过,样式代码后附,我们直接看如何引入element的代码

虽然官网已经有了演示代码,但是我们要简单了解一下,每一步的意义

如上的演示,我们很清楚需要利用的是input、button这种表单的样式,所以需要使用到<el-input><el-input/> <el-button><el-button/>按钮是为了最后判断是否满足后台数据(此案例没有),也可以一起判断是否满足所有的规则,需要使用点击事件触发绑定@click

第一步在新的组件中写入以下代码,也可以说是路由组件

如上图在v-from中使用到了:model :rules来绑定,我需要在data中写入对应的绑定内容

 登录按钮判断是否全都满足

 在父组件APP中我们需要写入路由、占位符、给router-link绑定了v-show是为了让点击登录,登录按钮就会消失,其中也使用了element自带的美化按钮,直接使用,前提都是自定义安装中要有

router代码中注册

3.源码

APP.vue代码

  1. <template>
  2. <div id="app">
  3. <!-- 路由占位符 -->
  4. <router-link to="/login" v-show="isshow"> <el-button type="primary" class="button" >登录</el-button></router-link>
  5. <router-view></router-view>
  6. </div>
  7. </template>
  8. <script>
  9. import Login from './views/Login.vue';
  10. export default {
  11. name: "app",
  12. components: { Login },
  13. computed:{
  14. isshow(){
  15. return this.$route.name=='login'?false:true
  16. },
  17. }
  18. }
  19. </script>
  20. <style>
  21. @import url('./assets/style.css');
  22. .button{
  23. position: fixed;
  24. z-index: 9999999;
  25. }
  26. </style>

 路由组件代码

  1. <template>
  2. <div class="login_main">
  3. <div class="login_box">
  4. <!-- 登录左边广告框 -->
  5. <div class="l"></div>
  6. <div class="r">
  7. <h1>小余努力搬砖</h1>
  8. <el-form :model="form" :rules="forRules" ref="fromRef">
  9. <el-form-item prop="username">
  10. <el-input prefix-icon="el-icon-s-custom" v-model="form.username" placeholder="请输入用户名" ></el-input>
  11. </el-form-item>
  12. <el-form-item prop="password">
  13. <el-input prefix-icon="el-icon-view" type="password" v-model="form.password" placeholder="请输入密码"></el-input>
  14. </el-form-item>
  15. <el-button type="primary" @click="btn" style="width: 100%;">登录</el-button>
  16. </el-form>
  17. </div>
  18. <div class="c"></div>
  19. </div>
  20. </div>
  21. </template>
  22. <script>
  23. export default {
  24. name: 'Login',
  25. data() {
  26. return {
  27. form:{
  28. username:'',
  29. password:''
  30. },
  31. forRules:{
  32. username:[
  33. {required:true,message:'用户名不能为空',trigger:'blur'},
  34. {min:3,max:10,message:'请输入3~10个字符',trigger:'blur'}
  35. ],
  36. password:[
  37. {required:true,message:'密码不能为空',trigger:'blur'},
  38. {min:3,max:10,message:'请输入3~10个字符',trigger:'blur'}
  39. ]
  40. }
  41. };
  42. },
  43. mounted() {
  44. },
  45. btn(){
  46. // this.$refs.fromRef.validate(isOK=>{
  47. // if(isOK){
  48. // console.log('1');
  49. // }else{
  50. // alert('请正确输入')
  51. // }
  52. // })
  53. this.$refs.fromRef.validate().then(()=>{
  54. }).catch(()=>{
  55. // alert('请正确输入')
  56. })
  57. }
  58. },
  59. };
  60. </script>
  61. <style scoped>
  62. .login_main{
  63. height: 100%;
  64. width: 100%;
  65. background: url(../assets/img/login_bg.jpg) no-repeat;
  66. background-size: cover;
  67. }
  68. .login_box{
  69. width: 900px;
  70. height:515px;
  71. background: #fff;
  72. box-shadow: 0px 0px 10px 5px #ddebfe;
  73. position: absolute;
  74. top:50%;
  75. left:50%;
  76. transform: translate(-50%,-50%);
  77. border-radius: 12px 0px 0px 12px;
  78. }
  79. .login_box .l{
  80. background: url(../assets/img/login_left.jpg) no-repeat;
  81. width: 350px;
  82. height: 515px;
  83. border-radius: 12px 0px 0px 12px;
  84. }
  85. .login_box .r{
  86. width: 550px;
  87. box-sizing: border-box;
  88. padding: 30px;
  89. }
  90. h1{
  91. text-align: center;
  92. color: #4c4c4c;
  93. }
  94. .button{
  95. width: 100%;
  96. }
  97. </style>

router代码

  1. routes:[
  2. // 注册组件
  3. // {path:'/',redirect:'/login'},
  4. {name:'login',path:'/login',component:Login}
  5. ]
  6. })

assets css代码引入到app.vue中

  1. html,body,div,ul,p{
  2. margin: 0;
  3. padding: 0;
  4. }
  5. html,body,#app{
  6. height: 100%;
  7. }
  8. .l{
  9. float: left;
  10. }
  11. .r{
  12. float: right;
  13. }
  14. .c{clear: both;}

两张图片也放在assets使用时引入,如下点击自取图片

结束语:分享到此就结束啦,有问题随时私聊

网站建设定制开发 软件系统开发定制 定制软件开发 软件开发定制 定制app开发 app开发定制 app开发定制公司 电商商城定制开发 定制小程序开发 定制开发小程序 客户管理系统开发定制 定制网站 定制开发 crm开发定制 开发公司 小程序开发定制 定制软件 收款定制开发 企业网站定制开发 定制化开发 android系统定制开发 定制小程序开发费用 定制设计 专注app软件定制开发 软件开发定制定制 知名网站建设定制 软件定制开发供应商 应用系统定制开发 软件系统定制开发 企业管理系统定制开发 系统定制开发