VUE realizes registration and login effects

VUE realizes registration and login effects

This article example shares the specific code of VUE to achieve registration and login effects for your reference. The specific content is as follows

1. Effect display

2. Registration effect realization

<template>
  <div class="login-section">
    <el-form 
      label-position="top" label-width="100px" class="demo-ruleForm"
      :rules="rules"
      :model="rulesForm"
        status-icon
        ref="ruleForm"
    >
      <el-form-item label="username" prop="name">
        <el-input type="text" v-model="rulesForm.name"></el-input>
      </el-form-item>
      <el-form-item label="password" prop="password">
        <el-input type="password" v-model="rulesForm.password"></el-input>
      </el-form-item>
 
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')" >Submit</el-button>
        <el-button >Reset</el-button>
      </el-form-item>
 
    </el-form>
  </div>
</template>
<script>
import {register} from '@/service/api';
export default {
  data() {
    return {
      rulesForm:{
        name:'',
        password:''
      },
      rules:
        name:[
          {required:true,message:'Please enter your name',trigger:"blur"},
          {min:1,max:5,message:"length 3-5",trigger:"blur"}
        ],
        password:[
          {required:true,message:'Please enter your password',trigger:"blur"},
          {min:3,max:5,message:"length 3-5",trigger:"blur"}
        ]
      }
    };
  },
  methods: {
    submitForm(formName){
      this.$refs[formName].validate((valid)=>{
          if(valid){
              //If the check passes, send the username and password to the backend register({
                name: this.rulesForm.name,
                password: this.rulesForm.password,
              }).then((data)=>{
                console.log(data)
                if(data.code === 0){
                  localStorage.setItem('token',data.data.token);
                    window.location.href= '/';
                }
                if(data.code === 1){
                  this.$message.error(data.message)
                }
              });
          }else{
            console.log("error submit!!");
            return false;
          }
      });
    }
  }
}
</script>
 
<style lang="stylus">
.login-section
  padding 0px 20px
</style>

3. Login page implementation

<template>
  <div class="login-section">
    <el-form
      label-position="top"
      label-width="100px" class="demo-ruleForm"
      :rules="rules"
      :model="rulesForm"
        status-icon
        ref="ruleForm"
    >
      <el-form-item label="username" prop="name">
        <el-input type="text" v-model="rulesForm.name"></el-input>
      </el-form-item>
      <el-form-item label="password" prop="password">
        <el-input type="password" v-model="rulesForm.password"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">Submit</el-button>
        <el-button>Reset</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import {login} from '@/service/api';
 
export default {
  data() {
    return {
      //Object for storing data rulesForm:{
        name:'',
        password:''
      },
      rules:
        name:[
          {required:true,message:'Please enter your name',trigger:"blur"},
          {min:1,max:5,message:"length 3-5",trigger:"blur"}
        ],
        password:[
          {required:true,message:'Please enter your password',trigger:"blur"},
          {min:3,max:5,message:"length 3-5",trigger:"blur"}
        ]
      }
    };
  },
  methods: {
    submitForm(formName){
      this.$refs[formName].validate((valid)=>{
          if(valid){
              //If the check passes, send the username and password to the backend login({
                name: this.rulesForm.name,
                password: this.rulesForm.password,
              }).then((data)=>{
                console.log(data)
                if(data.code === 0){
                  localStorage.setItem('token',data.data.token);
                    window.location.href= '/';
                }
                if(data.code === 1){
                  this.$message.error(data.message)
                }
              });
          }else{
            console.log("error submit!!");
            return false;
          }
      });
    }
  }
}
</script>
 
<style lang="stylus">
.login-section
  padding 0px 20px
</style>

4. Routing jump implementation

import Vue from 'vue'
import Router from 'vue-router'
Vue.use(Router)
import Store from '@/store'
import {userInfo} from '@/service/api.js'
import Login from '@/views/user-login/index.vue'
const router = new Router({
    mode:"history",
    routes:[
        {
            path:'/login',
            name:"login",
            title:"Login Page",
            component:Login,
            meta:{
                login:true
            }
        }
    ]
});
//Route guard router.beforeEach( async (to,from,next) => {
    /*
        Some routes require login, to determine the login status 1. Not logged in: jump to the login page 2. Login: directly enter Some routes do not require login, directly enter ps: whether to log in --meta
    */
    const token = localStorage.getItem('token');
    const isLogin = !!token;
    //When entering the route, you need to send a token to the backend to verify whether it is legal const data = await userInfo();
    Store.commit('chageUserInfo',data.data)
 
   if(to.matched.some(item => item.meta.login)){//Login requiredconsole.log("Login required");
 
        if(isLogin){//Already logged in, directly pass if(data.error === 400){//The backend tells you that the login failed next({name:'login'});
                localStorage.removeItem('token');
                return;
            }
            if(to.name === 'login'){
                next({name:'home'});
            }else{
                next();
            }
            return;
        }
        if(!isLogin && to.name === 'login'){//Not logged in, but going to the login page next();
        }
        if(!isLogin && to.name !== 'login'){//Not logged in, not the login page next({name:'login'});
        }
   }else{
       next();
   }
})
export default router;

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Example of implementing login and logout in Vue
  • Vue2.0 axios front-end and back-end login interceptor (example explanation)
  • Sample code for vue+axios novice practice to implement login
  • Detailed explanation of WeChat authorization login using vue.js and laravel
  • Vue front-end implementation of login interception and use of axios interceptor
  • Implementing the login page based on vue-cli3 and element
  • Vue learning road login registration example code
  • vue implements the registration function of sending SMS verification code through mobile phone
  • Vue.js implements user comment, login, registration, and information modification functions
  • Vue project realizes login and registration effect

<<:  How to restore a single database or table in MySQL and possible pitfalls

>>:  Detailed explanation of the implementation process of building a kernel tree in Ubuntu 12.04

Recommend

How to use Nginx proxy to surf the Internet

I usually use nginx as a reverse proxy for tomcat...

How to solve the DOS window garbled problem in MySQL

The garbled code problem is as follows: The reaso...

HTML Tutorial: Collection of commonly used HTML tags (5)

Related articles: Beginners learn some HTML tags ...

How to use vue filter

Table of contents Overview Defining filters Use o...

Basic operations on invisible columns in MySQL 8.0

Table of contents 01 Create invisible columns 02 ...

vue3.0+echarts realizes three-dimensional column chart

Preface: Vue3.0 implements echarts three-dimensio...

Create a custom system tray indicator for your tasks on Linux

System tray icons are still a magical feature tod...

Docker connection mongodb implementation process and code examples

After the container is started Log in to admin fi...

Ubuntu20.04 VNC installation and configuration implementation

VNC is a remote desktop protocol. Follow the inst...

PHP-HTMLhtml important knowledge points notes (must read)

1. Use frameset, frame and iframe to realize mult...

JavaScript and JQuery Framework Basics Tutorial

Table of contents 1. JS Object DOM –1, Function –...

How to quickly install tensorflow environment in Docker

Quickly install the tensorflow environment in Doc...

Design Theory: Textual Expression and Usability

<br />In text design, we usually focus on th...