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

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;

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

相關(guān)文章

最新評(píng)論

浙江省| 满洲里市| 宁南县| 余江县| 通化市| 临沭县| 灵武市| 尤溪县| 镇远县| 阜南县| 叶城县| 洪泽县| 台前县| 聂拉木县| 连云港市| 镇远县| 确山县| 墨玉县| 潜山县| 茶陵县| 灌阳县| 古蔺县| 武鸣县| 邢台市| 民勤县| 城市| 浦北县| 南汇区| 建湖县| 丹凤县| 潮安县| 龙州县| 福清市| 洛隆县| 曲松县| 周宁县| 塔河县| 巴塘县| 江北区| 广宗县| 绵竹市|