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


    一、搭建项目

    1.1 安装 Element-UI

    先确保是否安装了vue-cli脚手架工具 !!!

    安装vue脚手架可以看看我的上一篇博客

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

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

    1.2 导入组件

    打开 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

    1.3 创建登录、注册界面

    我在src目录下新建了一个views专门存放一些界面组件,界面可自己设计,以下是我编写的:

    1. 创建用户登录组件Login.vue

    1. <script>
    2. export default {
    3. name: 'Login',
    4. data() {
    5. return {
    6. username: '',
    7. password: ''
    8. }
    9. },
    10. methods: {
    11. gotoRegister() {
    12. this.$router.push("/Register");
    13. }
    14. }
    15. }
    16. script>
    17. <style scoped>
    18. .login-wrap {
    19. box-sizing: border-box;
    20. width: 100%;
    21. height: 100%;
    22. padding-top: 10%;
    23. background-color: #3b7cf5;
    24. background-repeat: no-repeat;
    25. background-position: center right;
    26. background-size: 100%;
    27. }
    28. .login-container {
    29. border-radius: 10px;
    30. margin: 0px auto;
    31. width: 350px;
    32. padding: 30px 35px 15px 35px;
    33. border: 1px solid #eaeaea;
    34. text-align: left;
    35. background-color: rgba(229, 229, 229, 0.8);
    36. }
    37. .title {
    38. margin: 0px auto 40px auto;
    39. text-align: center;
    40. color: #0b0b0b;
    41. }
    42. style>

     创建用户注册组件Register.vue

    1. <script>
    2. export default {
    3. name: 'Register',
    4. data() {
    5. return {
    6. username: '',
    7. password: ''
    8. }
    9. },
    10. methods: {
    11. gotoLogin() {
    12. this.$router.push("/");
    13. }
    14. }
    15. }
    16. script>
    17. <style scoped>
    18. .login-wrap {
    19. box-sizing: border-box;
    20. width: 100%;
    21. height: 100%;
    22. padding-top: 10%;
    23. background-color: #3b7cf5;
    24. background-repeat: no-repeat;
    25. background-position: center right;
    26. background-size: 100%;
    27. }
    28. .login-container {
    29. border-radius: 10px;
    30. margin: 0px auto;
    31. width: 350px;
    32. padding: 30px 35px 15px 35px;
    33. border: 1px solid #eaeaea;
    34. text-align: left;
    35. background-color: rgba(229, 229, 229, 0.8);
    36. }
    37. .title {
    38. margin: 0px auto 40px auto;
    39. text-align: center;
    40. color: #0b0b0b;
    41. }
    42. style>

    注1: