最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

VUE實現(xiàn)注冊與登錄效果

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

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

1.效果展示

2.注冊效果實現(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:'請輸入名字',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){
              //如果校檢通過,在這里向后端發(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.登錄頁面實現(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 {
      //存儲數(shù)據(jù)的對象
      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){
              //如果校檢通過,在這里向后端發(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)實現(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.登錄:直接進入
        有些路由是不需要登錄的,直接進入
        ps:是否需要登錄 --meta
    */
    const token = localStorage.getItem('token');
    const isLogin = !!token;
    //進入路由的時候,需要向后端發(fā)送token,驗證是否合法
    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)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • uniapp引入模塊化js文件和非模塊化js文件的四種方式

    uniapp引入模塊化js文件和非模塊化js文件的四種方式

    這篇文章主要介紹了uniapp引入模塊化js文件和非模塊化js文件的四種方式,本文結(jié)合實例代碼給大家講解的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-11-11
  • elementUI中el-upload文件上傳的實現(xiàn)方法

    elementUI中el-upload文件上傳的實現(xiàn)方法

    ElementUI的組件支持多種事件鉤子,如http-request、before-upload和on-change,以實現(xiàn)自定義文件上傳處理,這篇文章主要介紹了elementUI中el-upload文件上傳的實現(xiàn)方法,需要的朋友可以參考下
    2024-11-11
  • vue 過濾器和自定義指令的使用

    vue 過濾器和自定義指令的使用

    本文主要介紹Vue.js中過濾器和自定義指令相關的知識點,包括過濾器的定義方式,和使用方法,以及自定義指令的概念和注冊方式。
    2021-05-05
  • vue項目使用高德地圖時報錯:AMap?is?not?defined解決辦法

    vue項目使用高德地圖時報錯:AMap?is?not?defined解決辦法

    這篇文章主要給大家介紹了關于vue項目使用高德地圖時報錯:AMap?is?not?defined的解決辦法,"AMap is not defined"是一個錯誤提示,意思是在代碼中沒有找到定義的AMap,需要的朋友可以參考下
    2023-12-12
  • 使用vue.js實現(xiàn)聯(lián)動效果的示例代碼

    使用vue.js實現(xiàn)聯(lián)動效果的示例代碼

    本篇文章主要介紹了使用vue.js實現(xiàn)聯(lián)動效果的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-01-01
  • vue2.0 datepicker使用方法

    vue2.0 datepicker使用方法

    這篇文章主要介紹了vue2.0 datepicker的使用方法,非常不錯,具有參考借鑒借鑒價值,需要的朋友參考下
    2018-02-02
  • VUE3自定義指令防止重復點擊多次提交的實現(xiàn)方法

    VUE3自定義指令防止重復點擊多次提交的實現(xiàn)方法

    vue3項目,新增彈框連續(xù)點擊確定按鈕防止多次提交,在按鈕上添加自定義指令,這篇文章主要介紹了VUE3自定義指令防止重復點擊多次提交的實現(xiàn)方法,需要的朋友可以參考下
    2024-08-08
  • vue組件常用的父子、兄弟、跨組件等傳值方法

    vue組件常用的父子、兄弟、跨組件等傳值方法

    Vue常用的三種傳值方式有:??父傳子??子傳父??非父子傳值?引用官網(wǎng)的一句話:父子組件的關系可以總結(jié)為?prop?向下傳遞,事件向上傳遞,熟悉vue各類關系的組件之間傳值方法會令開發(fā)更加得心應手,下面將對父子、兄弟、頁級組件之間的傳值作淺談
    2023-12-12
  • vue實現(xiàn)彈窗引用另一個頁面窗口

    vue實現(xiàn)彈窗引用另一個頁面窗口

    這篇文章主要介紹了vue實現(xiàn)彈窗引用另一個頁面窗口,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-04-04
  • vue-element-admin搭建后臺管理系統(tǒng)的實現(xiàn)步驟

    vue-element-admin搭建后臺管理系統(tǒng)的實現(xiàn)步驟

    本文主要介紹了vue-element-admin搭建后臺管理系統(tǒng)的實現(xiàn)步驟,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-10-10

最新評論

贡山| 镇沅| 安新县| 兰州市| 全椒县| 连江县| 色达县| 四川省| 马尔康县| 绿春县| 昭平县| 天水市| 加查县| 安多县| 进贤县| 安平县| 谢通门县| 汉寿县| 喀喇沁旗| 措勤县| 呼玛县| 康乐县| 内黄县| 秀山| 江西省| 靖边县| 绥滨县| 全州县| 富川| 海门市| 乌兰县| 双城市| 株洲县| 镇巴县| 方山县| 宁国市| 洪雅县| 手游| 依安县| 安阳市| 鹤山市|