使用element-ui实现后台的用户登录

今天主要写了后台的用户登录,下面就说说怎么实现用户的登录。

效果:
在这里插入图片描述
代码实现:

1、首先就是页面的布局(主要是用element-ui来实现的)

<template>
  <div class="login_box">
    <div class="login_content">
      <h1>用户登录</h1>
      <el-form
        :model="ruleForm"
        status-icon
        :rules="rules"
        ref="ruleForm"
        label-width="65px"
        class="demo-ruleForm"
      >
        <el-form-item label="用户名" prop="username">
          <el-input type="text" v-model="ruleForm.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input type="password" v-model="ruleForm.password" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm">登录</el-button>
          <el-button @click="resetForm" class="test">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
// 引入接口
import { login } from "../../http/api";
export default {
  data() {
    return {
      ruleForm: {
        username: "",
        password: ""
      },
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 5, max: 20, message: "长度在 5 到 20 个字符", trigger: "blur" }
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 6, max: 20, message: "长度在 6 到 20 个字符", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    async submitForm() {
      /**
       * 调用登录接口
       * 成功:
       *     保存token
       *     跳转到首页,并给出成功的提示
       * 失败:给出错误提示,让用户重新登录
       */
      const res = await login(this.ruleForm);
      const {
        meta: { msg, status }
      } = res.data;
      if (status == 200) {
        const { token } = res.data.data;
        localStorage.setItem("token", token);
        //记住上次没有token要访问的页面地址,如果登录成功,再返回到上次要访问到页面
        const { redirect } = this.$route.query;

        //如果直接登录,没有redirect,成功后直接跳转到home
        if (!redirect) {
          this.$router.push({ name: "home" });
        } else {
          this.$router.push({ path: redirect });
        }

        this.$message({
          message: msg,
          type: "success"
        });
      } else {
        this.$message({
          message: msg,
          type: "error"
        });
      }
      console.log(res);
    },
    resetForm() {
      console.log("重置");
      this.ruleForm.username = "";
      this.ruleForm.password = "";
    }
  }
};
</script>

<style lang="scss" scoped>
.login_box {
  width: 100%;
  height: 100%;
  background-color: #324057;
  position: relative;
  .login_content {
    width: 35%;
    height: 350px;
    background-color: white;
    border-radius: 10px;
    overflow: hidden;
    position: absolute;
    top: 25%;
    left: 30%;
    h1 {
      text-align: center;
      width: 100%;
      height: 40px;
      line-height: 40px;
    }
    .demo-ruleForm {
      padding: 30px;
      .test {
        background-color: #999;
        color: white;
      }
    }
  }
}
</style>

2、接着就是网络层的一个封装

在src的目录下,建一个文件夹(http)

a、在http下建一个request.js文件,主要就是引入axios,创建axios实例,设置拦截请求和响应请求

import axios from "axios";

const service = axios.create({
  baseURL: "https://www.liulongbin.top:8888/api/private/v1",
  timeout: 30000
});

// 请求拦截器
axios.interceptors.request.use(
  function(config) {
    return config;
  },
  function(error) {
    return Promise.reject(error);
  }
);

// 响应拦截器
axios.interceptors.response.use(
  function(config) {
    return config;
  },
  function(error) {
    return Promise.reject(error);
  }
);

export default service;

b、接着就是对api的一个封装

import request from "./requset";

/**
 * 登录接口
 */

export function login(data) {
  console.log("接口", data);
  return request({
    url: "login",
    method: "POST",
    data
  });
}

3、给登录一个点击事件,之后就是调用接口,实现功能。

  async submitForm() {
      /**
       * 调用登录接口
       * 成功:
       *     保存token
       *     跳转到首页,并给出成功的提示
       * 失败:给出错误提示,让用户重新登录
       */
      const res = await login(this.ruleForm);
      const {
        meta: { msg, status }
      } = res.data;
      if (status == 200) {
        const { token } = res.data.data;
        localStorage.setItem("token", token);
        //记住上次没有token要访问的页面地址,如果登录成功,再返回到上次要访问到页面
        const { redirect } = this.$route.query;

        //如果直接登录,没有redirect,成功后直接跳转到home
        if (!redirect) {
          this.$router.push({ name: "home" });
        } else {
          this.$router.push({ path: redirect });
        }

        this.$message({
          message: msg,
          type: "success"
        });
      } else {
        this.$message({
          message: msg,
          type: "error"
        });
      }
      console.log(res);
    },

上面的就可以实现登录了

4、接着就说说路由拦截这一块(在main.js里面写)

// 全局路由l拦截
router.beforeEach((to, from, next) => {
  /* 路由发生变化修改页面title */
  if (to.meta.title) {
    document.title = to.meta.title;
  }
  next();

  // 路由拦截
  if (to.meta.auth) {
    const token = localStorage.getItem("token");
    if (!token) {
      next({
        name: "login",
        query: {
          redirect: to.fullPath
        }
      });
    } else {
      next();
    }
  } else {
    next();
  }
});

这个auth就是在路由的js文件里面设置的一个属性,为true。

在这里插入图片描述

  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue.js是一款流行的JavaScript框架,它的灵活性和易用性使其成为构建用户界面的首选工具。Element-UI则是一个基于Vue.js的一套桌面端UI组件库,它提供了丰富的UI组件,使得搭建管理后台变得更加简单。 要使用Element-UI搭建管理后台,首先需要在Vue项目中安装Element-UI。可以通过npm或者yarn命令来安装,具体的安装命令可以在官方文档中找到。 安装完成后,在项目中引入Element-UI。可以在main.js文件中添加以下代码来全局引入Element-UI的样式和组件: ``` import ElementUI from 'element-ui'; import 'element-ui/lib/theme-chalk/index.css'; Vue.use(ElementUI); ``` 接下来就可以在Vue组件中使用Element-UI的组件了。例如,可以在组件的模板中添加一个按钮: ``` <el-button>点击我</el-button> ``` 使用Element-UI的组件和样式,可以轻松实现页面的布局和美化。其中包括菜单、表格、表单、弹窗等各种常用的管理后台组件。 Element-UI还提供了丰富的交互和功能组件,例如消息提示、弹窗确认框、分页等。可以根据具体需求在组件中使用这些功能。 在搭建管理后台时,还可以利用Element-UI的路由和菜单组件来实现页面的导航。可以通过路由配置文件来定义不同页面的路由路径,然后在菜单组件中使用路由链接来跳转到不同的页面。 总之,使用Element-UI搭建管理后台是非常简单和高效的。通过引入Element-UI的样式和组件,可以快速搭建出功能丰富、界面美观的管理后台

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值