Project prospect: Background management system is necessary for the company. A few years ago, due to the rare technical personnel, the background system was very expensive. In recent years, with the rapid development of THE IT industry, some small and medium-sized companies began to write and maintain the background by themselves, so as to maximize profits. Today is a simple background login operation

Demonstration effect:


1. Page layout (Vue+Element Ui)

Second, the login process

4. Jump to the home page, and give a success message. 5. Failure: give an error message, let the user re-login specific code: create folder to encapsulate axios request

import axios from "axios";

const service = axios.create({
  baseURL: "Url".timeout: 3000

// Request the interceptor
  function (config) {
    // If it is not a login page, it must carry the token to the back end to return data

    // If it is not the login page, obtain the token and carry it to the back end through the request header
    if(config.url ! = ='login') {
      const token = localStorage.getItem('token');

      // Set the request header
      config.headers['Authorization'] = token;

    return config;
  function(error) {
    return Promise.reject(error); });// Respond to interceptors
  function(response) {
    return response;
  function(error) {
    return Promise.reject(error); });export default service;

Copy the code

Request Interface API

import request from "./index";

export function login(data) {
  // console.log('data::',data)
  return request({
    url: "login".method: "POST",
Copy the code

Do global route interception in main.js

// Global route interception
router.beforeEach((to, from, next) = > {
  console.log("to:", to);
  if (to.meta.auth) {
    const token = localStorage.getItem('token')
    if(! token) { next({name: 'login'.query: { redirect:to.fullPath  }
    } else {
  } else{ next(); }})Copy the code

Login page code

  <div class="login_wrap">
      <h1>The user login</h1>
      <el-form-item label="User name" prop="username">
        <el-input v-model="userinfo.username"></el-input>
      <el-form-item label="Password" prop="password">
        <el-input v-model="userinfo.password" type="password"></el-input>
      <el-button type="primary" class="login_btn" @click.prevent="handleLogin"
        >Login < / el - button ></el-form>

// Import the login interface
import { login } from "@/http/api";
export default {
  name: "login".data() {
    return {
      userinfo: {
        username: "".password: ""
      loginRules: {
        username: [{required: true.message: "Please enter your user name.".trigger: "blur" },
          { min: 5.max: 20.message: "5 to 20 characters in length.".trigger: "blur"}].password: [{required: true.message: "Please enter your password".trigger: "blur" },
          { min: 5.max: 60.message: "5 to 20 characters in length.".trigger: "blur"}}}; },methods: {
    async handleLogin() {
      /** * Invoke the login interface * Successful: * save the token * jump to the home page and give a success message * Failed: give an error message and let the user log in again */
      const res = await login(this.userinfo);
      const {
        meta: { msg, status }
      } =;

      if (status === 200) {
        const { token } =;
        localStorage.setItem("token", token);
        // Remember the last time there is no token to access the page address, if the login is successful, then return to the last time to access the page
        const { redirect } = this.$route.query;

        // If you log in directly and do not redirect, redirect directly to the home
        if(! redirect) {this.$router.push({ name: "Home" });
        } else {
          this.$router.push({ path: redirect });

          message: msg,
          type: "success"
      } else {
          message: msg,
          type: "error"}); }}}};</script>

<style lang="scss" scoped>
.login_wrap {
  width: 100%;
  height: 100%;
  background: # 072765;

.login_form {
  width: 40%;
  height: 260px;
  padding: 30px;
  background: #fff;
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  top: 0;
  margin: auto;

  h1 {
    text-align: center;
    margin: 10px 0; }}.login_btn {
  width: 100%;

Copy the code

Summary: The above is a background management system to achieve the login process, specific please combine with THE API to achieve, the main core is to determine whether there is a token and token expiration time limit, and then clear the token to log out.