• ElementUI实现登录注册啊,axios全局配置,CORS跨域


    一,项目搭建

    认识ElementUI

    ElementUI是一个基于Vue.js 2.0的桌面端组件库,它提供了一套丰富的UI组件,包括表格、表单、弹框、按钮、菜单等常用组件,具备易用、美观、高效、灵活等优势,能够极大的提高Web应用的开发效率。ElementUI的文档非常详细,示例丰富,易于入手,同时也支持自定义主题,开发者可以根据自己的需要进行调整。ElementUI同时也支持按需加载,可以减少项目体积,提高网页加载速度。由于其易用性和高效性,ElementUI已成为Vue.js开发的首选UI组件库之一。

    2.安装ElementUI

    安装ElementUI必须借助于vue-cli工具如果没有可观看我上一篇博客

    构建好项目后通过npm安装element-ui

    cd 项目根路径                               #进入新建项目的根目录
    npm install element-ui -S                  #安装element-ui模块

     下载好了后进入项目查看package.json查看是否成功(因为有一次某人下载好了cmd窗口并没有提示下载成功)

     3.导入组件

    打开 src目录下的main.js,该文件是项目的入口文件,所以在这里导入,其他组件均可使用,不用再导入。

    1. import Vue from 'vue'
    2. //新添加1
    3. import ElementUI from 'element-ui'
    4. //新增加2,避免后期打包样式不同,要放在import App from './App';之前
    5. import 'element-ui/lib/theme-chalk/index.css'
    6. import App from './App'
    7. import router from './router'
    8. Vue.use(ElementUI) //新添加3
    9. Vue.config.productionTip = false

     4.创建登录、注册界面

    在目录下新建了一个views专门存放一些界面组件,界面可自己设计,列如:

    登录界面:

    1. <template>
    2. <div class="login-wrap">
    3. <el-form class="login-container">
    4. <h1 class="title">用户登录h1>
    5. <el-form-item label="">
    6. <el-input type="text" v-model="username" placeholder="登录账号" autocomplete="off">el-input>
    7. el-form-item>
    8. <el-form-item label="">
    9. <el-input type="password" v-model="password" placeholder="登录密码" autocomplete="off">el-input>
    10. el-form-item>
    11. <el-form-item>
    12. <el-button type="warning" style="width:100%;" @click="doSubmit()">提交el-button>
    13. el-form-item>
    14. <el-row style="text-align: center;margin-top:-10px">
    15. <el-link type="primary">忘记密码el-link>
    16. <el-link type="primary" @click="gotoRegister()">用户注册el-link>
    17. el-row>
    18. el-form>
    19. div>
    20. template>
    21. <script>
    22. export default {
    23. name: 'Login',
    24. data() {
    25. return {
    26. username: '',
    27. password: ''
    28. }
    29. },
    30. methods: {
    31. gotoRegister() {
    32. this.$router.push("/Register");
    33. }
    34. }
    35. }
    36. script>
    37. <style scoped>
    38. .login-wrap {
    39. box-sizing: border-box;
    40. width: 100%;
    41. height: 100%;
    42. padding-top: 10%;
    43. /* background-color: #3b7cf5; */
    44. background-repeat: no-repeat;
    45. background-position: center right;
    46. background-size: 100%;
    47. }
    48. .login-container {
    49. border-radius: 10px;
    50. margin: 0px auto;
    51. width: 350px;
    52. padding: 30px 35px 15px 35px;
    53. border: 1px solid #eaeaea;
    54. text-align: left;
    55. background-color: rgba(229, 229, 229, 0.8);
    56. }
    57. .title {
    58. margin: 0px auto 40px auto;
    59. text-align: center;
    60. color: #0b0b0b;
    61. }
    62. style>

    注册界面:

    1. <template>
    2. <div class="login-wrap">
    3. <el-form class="login-container">
    4. <h1 class="title">用户注册h1>
    5. <el-form-item label="">
    6. <el-input type="text" v-model="username" placeholder="注册账号" autocomplete="off">el-input>
    7. el-form-item>
    8. <el-form-item label="">
    9. <el-input type="password" v-model="password" placeholder="注册密码" autocomplete="off">el-input>
    10. el-form-item>
    11. <el-form-item>
    12. <el-button type="warning" style="width:100%;" @click="doSubmit()">提交el-button>
    13. el-form-item>
    14. <el-row style="text-align: center;margin-top:-10px">
    15. <el-link type="primary">忘记密码el-link>
    16. <el-link type="primary" @click="gotoLogin()">用户注册el-link>
    17. el-row>
    18. el-form>
    19. div>
    20. template>
    21. <script>
    22. export default {
    23. name: 'Register',
    24. data() {
    25. return {
    26. username: '',
    27. password: ''
    28. }
    29. },
    30. methods: {
    31. gotoLogin() {
    32. this.$router.push("/");
    33. }
    34. }
    35. }
    36. script>
    37. <style scoped>
    38. .login-wrap {
    39. box-sizing: border-box;
    40. width: 100%;
    41. height: 100%;
    42. padding-top: 10%;
    43. ;
    44. /* background-color: #3b7cf5; */
    45. background-repeat: no-repeat;
    46. background-position: center right;
    47. background-size: 100%;
    48. }
    49. .login-container {
    50. border-radius: 10px;
    51. margin: 0px auto;
    52. width: 350px;
    53. padding: 30px 35px 15px 35px;
    54. border: 1px solid #eaeaea;
    55. text-align: left;
    56. background-color: rgba(229, 229, 229, 0.8);
    57. }
    58. .title {
    59. margin: 0px auto 40px auto;
    60. text-align: center;
    61. color: #0b0b0b;
    62. }
    63. style>

    注1: