Vue project realizes login and registration effect

Vue project realizes login and registration effect

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

Main content

The goal of this chapter: vue+element-ui completes registration and login

1. Effect display

2. View page: views

Registration page effect realization:

<template>
  <div class="login-section">
    <!-- :rules="rules" -->
    <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 {
      rulesForm:{
        name:'',
        password:''
      },
      rules:
        name:[
            {required:true,message:'The name is not written',trigger:'blur'},
            {min:1,max:5,message:'Length is between 3 and 5 characters'}
        ],
         password:[
            {required:true,message:'The name is not written',trigger:'blur'},
            {min:3,max:5,message:'Length is between 3 and 5 characters'}
        ]
      }
    };
  },
  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>

login.vue

<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 {
      rulesForm:{
        name:'',
        password:''
      },
      rules:
        name:[
            {required:true,message:'The name is not written',trigger:'blur'},
            {min:1,max:5,message:'Length is between 3 and 5 characters'}
        ],
         password:[
            {required:true,message:'The name is not written',trigger:'blur'},
            {min:3,max:5,message:'Length is between 3 and 5 characters'}
        ]
      }
    };
  },
  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>

Routing: index.js

import Vue from 'vue'
import Router from 'vue-router'
Vue.use(Router)
import Store from '@/store'
import {userInfo} from '@/service/api.js'
import Home from '@/views/home/Home.vue'
import Login from '@/views/user-login/index.vue'
const router = new Router({
    mode:"history",
    routes:[
        {
            path:'/',
            name:"Home",
            title:"Home",
            component:Home
        },
        {
            path:'/login',
            name:"login",
            title:"Login Page",
            component:Login,
            meta:{
                login:true
            }
        }
    ]
});
router.beforeEach( async (to,from,next) => {
    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 is requiredif(isLogin){//Already logged in, directly pass if(data.error === 400){//The backend tells you that the login failednext({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;

Summarize

That’s all for today’s content. It’s not very good because there are still some imperfections. We will continue to improve it later!

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 realizes registration and login effects

<<:  Three ways to communicate between Docker containers

>>:  Detailed explanation of the idea of ​​using mysqldump+expect+crontab to implement mysql periodic cold backup in linux

Recommend

HTML form tag usage learning tutorial

Forms in HTML can be used to collect various type...

Complete steps to implement face recognition login in Ubuntu

1. Install Howdy: howdy project address sudo add-...

How to use SVG icons in WeChat applets

SVG has been widely used in recent years due to i...

Three.js realizes Facebook Metaverse 3D dynamic logo effect

Table of contents background What is the Metavers...

JavaScript explains the encapsulation and use of slow-motion animation

Implementing process analysis (1) How to call rep...

Using the outline-offset property in CSS to implement a plus sign

Assume there is such an initial code: <!DOCTYP...

Detailed explanation of the difference between tags and elements in HTML

I believe that many friends who are new to web pag...

Complete steps to configure basic user authentication at the Nginx level

Preface Application scenario: probably the intern...

JavaScript anti-shake case study

principle The principle of anti-shake is: you can...

Mysql date formatting and complex date range query

Table of contents Preface Query usage scenario ca...

js code that associates the button with the enter key

Copy code The code is as follows: <html> &l...

HTML meta viewport attribute detailed description

What is a Viewport Mobile browsers place web pages...

Detailed explanation of the spacing problem between img tags

IMG tag basic analysis In HTML5, the img tag has ...

The difference between GB2312, GBK and UTF-8 in web page encoding

First of all, we need to understand that GB2312, ...

What are inline elements and block elements?

1. Inline elements only occupy the width of the co...