VUE實現註冊與登錄效果

本文實例為大傢分享瞭VUE實現註冊與登錄效果的具體代碼,供大傢參考,具體內容如下

1.效果展示

2.註冊效果實現

<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="用戶名" prop="name">
        <el-input type="text" v-model="rulesForm.name"></el-input>
      </el-form-item>
      <el-form-item label="密碼" 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')" >提交</el-button>
        <el-button >重置</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:'請輸入名字',trigger:"blur"},
          {min:1,max:5,message:"長度3-5",trigger:"blur"}
        ],
        password:[
          {required:true,message:'請輸入密碼',trigger:"blur"},
          {min:3,max:5,message:"長度3-5",trigger:"blur"}
        ]
      }
    };
  },
  methods: {
    submitForm(formName){
      this.$refs[formName].validate((valid)=>{
          if(valid){
              //如果校檢通過,在這裡向後端發送用戶名和密碼
              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.mes)
                }
              });
          }else{
            console.log("error submit!!");
            return false;
          }
      });
    }
  }
}
</script>
 
<style lang="stylus">
.login-section
  padding 0px 20px
</style>

3.登錄頁面實現

<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="用戶名" prop="name">
        <el-input type="text" v-model="rulesForm.name"></el-input>
      </el-form-item>
      <el-form-item label="密碼" 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')">提交</el-button>
        <el-button>重置</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:'請輸入名字',trigger:"blur"},
          {min:1,max:5,message:"長度3-5",trigger:"blur"}
        ],
        password:[
          {required:true,message:'請輸入密碼',trigger:"blur"},
          {min:3,max:5,message:"長度3-5",trigger:"blur"}
        ]
      }
    };
  },
  methods: {
    submitForm(formName){
      this.$refs[formName].validate((valid)=>{
          if(valid){
              //如果校檢通過,在這裡向後端發送用戶名和密碼
              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.mes)
                }
              });
          }else{
            console.log("error submit!!");
            return false;
          }
      });
    }
  }
}
</script>
 
<style lang="stylus">
.login-section
  padding 0px 20px
</style>

4.路由跳轉實現

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:"登錄頁",
            component:Login,
            meta:{
                login:true
            }
        }
    ]
});
//路由守衛
router.beforeEach( async (to,from,next) => {
    /*
        有些路由是需要登錄的,判斷登錄狀態
            1.沒有登錄:跳轉到登錄頁
            2.登錄:直接進入
        有些路由是不需要登錄的,直接進入
        ps:是否需要登錄 --meta
    */
    const token = localStorage.getItem('token');
    const isLogin = !!token;
    //進入路由的時候,需要向後端發送token,驗證是否合法
    const data = await userInfo();
    Store.commit('chageUserInfo',data.data)
 
   if(to.matched.some(item => item.meta.login)){//需要登錄
        console.log("需要登錄");
 
        if(isLogin){//已經登錄的,直接通過
            if(data.error === 400){//後端告訴你,登錄不成功
                next({name:'login'});
                localStorage.removeItem('token');
                return;
            }
            if(to.name === 'login'){
                next({name:'home'});
            }else{
                next();
            }
            return;
        }
        if(!isLogin && to.name === 'login'){//未登錄,但是要去登錄頁
            next();
        }
        if(!isLogin && to.name !== 'login'){//未登錄,去的也不是登錄頁
            next({name:'login'});
        }
   }else{
       next();
   }
})
export default router;

以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。

推薦閱讀: