欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

VUE實(shí)現(xiàn)注冊(cè)與登錄效果

 更新時(shí)間:2021年09月24日 08:55:55   作者:海賊王0101  
這篇文章主要為大家詳細(xì)介紹了VUE實(shí)現(xiàn)注冊(cè)與登錄效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了VUE實(shí)現(xiàn)注冊(cè)與登錄效果的具體代碼,供大家參考,具體內(nèi)容如下

1.效果展示

2.注冊(cè)效果實(shí)現(xiàn)

<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:'請(qǐng)輸入名字',trigger:"blur"},
          {min:1,max:5,message:"長度3-5",trigger:"blur"}
        ],
        password:[
          {required:true,message:'請(qǐng)輸入密碼',trigger:"blur"},
          {min:3,max:5,message:"長度3-5",trigger:"blur"}
        ]
      }
    };
  },
  methods: {
    submitForm(formName){
      this.$refs[formName].validate((valid)=>{
          if(valid){
              //如果校檢通過,在這里向后端發(fā)送用戶名和密碼
              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.登錄頁面實(shí)現(xiàn)

<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 {
      //存儲(chǔ)數(shù)據(jù)的對(duì)象
      rulesForm:{
        name:'',
        password:''
      },
      rules:{
        name:[
          {required:true,message:'請(qǐng)輸入名字',trigger:"blur"},
          {min:1,max:5,message:"長度3-5",trigger:"blur"}
        ],
        password:[
          {required:true,message:'請(qǐng)輸入密碼',trigger:"blur"},
          {min:3,max:5,message:"長度3-5",trigger:"blur"}
        ]
      }
    };
  },
  methods: {
    submitForm(formName){
      this.$refs[formName].validate((valid)=>{
          if(valid){
              //如果校檢通過,在這里向后端發(fā)送用戶名和密碼
              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.路由跳轉(zhuǎn)實(shí)現(xiàn)

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
            }
        }
    ]
});
//路由守衛(wèi)
router.beforeEach( async (to,from,next) => {
    /*
        有些路由是需要登錄的,判斷登錄狀態(tài)
            1.沒有登錄:跳轉(zhuǎn)到登錄頁
            2.登錄:直接進(jìn)入
        有些路由是不需要登錄的,直接進(jìn)入
        ps:是否需要登錄 --meta
    */
    const token = localStorage.getItem('token');
    const isLogin = !!token;
    //進(jìn)入路由的時(shí)候,需要向后端發(fā)送token,驗(yàn)證是否合法
    const data = await userInfo();
    Store.commit('chageUserInfo',data.data)
 
   if(to.matched.some(item => item.meta.login)){//需要登錄
        console.log("需要登錄");
 
        if(isLogin){//已經(jīng)登錄的,直接通過
            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;

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • vue3實(shí)現(xiàn)高德地圖天氣小組件

    vue3實(shí)現(xiàn)高德地圖天氣小組件

    這篇文章主要為大家詳細(xì)介紹了如何使用vue3實(shí)現(xiàn)一個(gè)高德地圖天氣小組件,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2024-10-10
  • Vue Elenent實(shí)現(xiàn)表格相同數(shù)據(jù)列合并

    Vue Elenent實(shí)現(xiàn)表格相同數(shù)據(jù)列合并

    這篇文章主要為大家詳細(xì)介紹了Vue Elenent實(shí)現(xiàn)表格相同數(shù)據(jù)列合并,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-11-11
  • vue2與vue3下如何訪問使用public下的文件

    vue2與vue3下如何訪問使用public下的文件

    這篇文章主要介紹了vue2與vue3下如何訪問使用public下的文件,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-05-05
  • Vue + element實(shí)現(xiàn)動(dòng)態(tài)顯示后臺(tái)數(shù)據(jù)到options的操作方法

    Vue + element實(shí)現(xiàn)動(dòng)態(tài)顯示后臺(tái)數(shù)據(jù)到options的操作方法

    最近遇到一個(gè)需求需要實(shí)現(xiàn)selector選擇器中選項(xiàng)值options 數(shù)據(jù)的動(dòng)態(tài)顯示,而非寫死的數(shù)據(jù),本文通過實(shí)例代碼給大家分享實(shí)現(xiàn)方法,感興趣的朋友一起看看吧
    2021-07-07
  • vue3基于elementplus 簡(jiǎn)單實(shí)現(xiàn)表格二次封裝過程

    vue3基于elementplus 簡(jiǎn)單實(shí)現(xiàn)表格二次封裝過程

    公司渲染表格數(shù)據(jù)時(shí)需要將空數(shù)據(jù)顯示‘-’,并且對(duì)于每一列數(shù)據(jù)的顯示也有一定的要求,基于這個(gè)需求對(duì)element-plus簡(jiǎn)單進(jìn)行了二次封裝,這篇文章主要介紹了vue3基于elementplus 簡(jiǎn)單實(shí)現(xiàn)表格二次封裝過程,需要的朋友可以參考下
    2024-05-05
  • vue3使用vuex實(shí)現(xiàn)頁面實(shí)時(shí)更新數(shù)據(jù)實(shí)例教程(setup)

    vue3使用vuex實(shí)現(xiàn)頁面實(shí)時(shí)更新數(shù)據(jù)實(shí)例教程(setup)

    在前端開發(fā)中往往會(huì)遇到頁面需要實(shí)時(shí)刷新數(shù)據(jù)的情況,給用戶最新的數(shù)據(jù)展示,這篇文章主要給大家介紹了關(guān)于vue3使用vuex實(shí)現(xiàn)頁面實(shí)時(shí)更新數(shù)據(jù)(setup)的相關(guān)資料,需要的朋友可以參考下
    2022-09-09
  • vue跳轉(zhuǎn)頁面打開新窗口,并攜帶與接收參數(shù)方式

    vue跳轉(zhuǎn)頁面打開新窗口,并攜帶與接收參數(shù)方式

    這篇文章主要介紹了vue跳轉(zhuǎn)頁面打開新窗口,并攜帶與接收參數(shù)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-04-04
  • vue3中ref獲取子組件的值代碼示例

    vue3中ref獲取子組件的值代碼示例

    這篇文章主要給大家介紹了關(guān)于vue3中ref獲取子組件值的相關(guān)資料,在Vue3中父組件獲取子組件的值可以通過使用'ref'和'$refs'來實(shí)現(xiàn),文中通過代碼示例介紹的非常詳細(xì),需要的朋友可以參考下
    2023-08-08
  • vue實(shí)現(xiàn)在線預(yù)覽office文件的示例代碼

    vue實(shí)現(xiàn)在線預(yù)覽office文件的示例代碼

    本文主要介紹了vue實(shí)現(xiàn)在線預(yù)覽office文件,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-10-10
  • 快速了解vue-cli 3.0 新特性

    快速了解vue-cli 3.0 新特性

    vue-cli 是 vue 官方團(tuán)隊(duì)推出的一款快速開發(fā) vue 項(xiàng)目的構(gòu)建工具,具有開箱即用并且提供簡(jiǎn)潔的自定義配置等功能。這篇文章主要介紹了快速了解vue-cli 3.0 新特性,需要的朋友可以參考下
    2018-02-02

最新評(píng)論