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

vue項目實現(xiàn)登陸注冊效果

 更新時間:2021年09月26日 08:49:45   作者:段嘉許..!  
這篇文章主要為大家詳細(xì)介紹了vue項目實現(xiàn)登陸注冊效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

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

主要內(nèi)容

本章目標(biāo):vue+element-ui完成注冊以及登陸

1.效果展示

2.視圖頁面:views

注冊頁面效果實現(xiàn):

<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="用戶名" 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位'}
        ],
         password:[
            {required:true,message:'名字沒寫',trigger:'blur'},
            {min:3,max:5,message:'長度在3到5位'}
        ]
      }
    };
  },
  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>

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="用戶名" 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位'}
        ],
         password:[
            {required:true,message:'名字沒寫',trigger:'blur'},
            {min:3,max:5,message:'長度在3到5位'}
        ]
      }
    };
  },
  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>

路由: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:"首頁",
            component:Home
        },
        {
            path:'/login',
            name:"login",
            title:"登錄頁",
            component:Login,
            meta:{
                login:true
            }
        }
    ]
});
router.beforeEach( async (to,from,next) => {
    const token = localStorage.getItem('token');
    const isLogin = !!token;
    //進(jìn)入路由的時候,需要向后端發(fā)送token,驗證是否合法
    const data = await userInfo();
    Store.commit('chageUserInfo',data.data)
   if(to.matched.some(item => item.meta.login)){//需要登錄
        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;

總結(jié)

今天的內(nèi)容就先到這里,因為還有一些沒完善所以不是很好,后面再繼續(xù)改進(jìn)!

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

相關(guān)文章

  • vue+d3js+fastapi實現(xiàn)天氣柱狀圖折線圖餅圖的示例

    vue+d3js+fastapi實現(xiàn)天氣柱狀圖折線圖餅圖的示例

    本文主要介紹了vue+d3js+fastapi實現(xiàn)天氣柱狀圖折線圖餅圖的示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2025-04-04
  • Vue語法和標(biāo)簽的入門使用教程

    Vue語法和標(biāo)簽的入門使用教程

    Vue是一套用于構(gòu)建用戶界面的漸進(jìn)式框架,下面這篇文章主要給大家介紹了關(guān)于Vue語法和標(biāo)簽使用的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-08-08
  • vue.js中父組件調(diào)用子組件的內(nèi)部方法示例

    vue.js中父組件調(diào)用子組件的內(nèi)部方法示例

    這篇文章主要給大家介紹了關(guān)于vue.js中父組件調(diào)用子組件內(nèi)部方法的相關(guān)資料,文中給出來了詳細(xì)的示例代碼供大家參考學(xué)習(xí),對大家的學(xué)習(xí)或者工作具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-10-10
  • vue3.0 加載json的方法(非ajax)

    vue3.0 加載json的方法(非ajax)

    這篇文章主要介紹了vue3.0 加載json的方法(非ajax),幫助大家更好的理解和學(xué)習(xí)vue,感興趣的朋友可以了解下
    2020-10-10
  • vue3循環(huán)設(shè)置ref并獲取的解決方案

    vue3循環(huán)設(shè)置ref并獲取的解決方案

    我們在平時做業(yè)務(wù)的時候,父子組件通信會經(jīng)常用到ref,這篇文章主要給大家介紹了關(guān)于vue3循環(huán)設(shè)置ref并獲取的解決方案,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2024-02-02
  • 解決打包后出現(xiàn)錯誤y.a.addRoute is not a function的問題

    解決打包后出現(xiàn)錯誤y.a.addRoute is not a function的

    這篇文章主要介紹了解決打包后出現(xiàn)y.a.addRoute is not a function的問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-10-10
  • vue中ref和reactive的區(qū)別及說明

    vue中ref和reactive的區(qū)別及說明

    這篇文章主要介紹了vue中ref和reactive的區(qū)別及說明,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-08-08
  • vue生命周期四個階段created和mount詳解

    vue生命周期四個階段created和mount詳解

    這篇文章主要介紹了vue生命周期四個階段created和mount,本文給大家介紹的非常詳細(xì),補充介紹了什么是實例,什么是實例被掛載到DOM,什么是dom,dao操作又是什么,感興趣的朋友跟隨小編一起看看吧
    2024-02-02
  • Element-UI 使用el-row 分欄布局的教程

    Element-UI 使用el-row 分欄布局的教程

    這篇文章主要介紹了Element-UI 使用el-row 分欄布局的教程,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-10-10
  • vite打包出現(xiàn)?"default"?is?not?exported?by?"node_modules/...問題解決辦法

    vite打包出現(xiàn)?"default"?is?not?exported?by?"

    這篇文章主要給大家介紹了關(guān)于vite打包出現(xiàn)?"default"?is?not?exported?by?"node_modules/...問題的解決辦法,文中通過代碼將解決的辦法介紹的非常詳細(xì),對同樣遇到這個問題的朋友具有一定的參考借鑒價值,需要的朋友可以參考下
    2024-06-06

最新評論

集安市| 商南县| 安西县| 孟津县| 许昌县| 平武县| 抚顺市| 综艺| 平果县| 团风县| 长治县| 伽师县| 安乡县| 曲周县| 无锡市| 临澧县| 云龙县| 沈丘县| 凭祥市| 娱乐| 河间市| 咸宁市| 邵东县| 新兴县| 贵州省| 南丹县| 海阳市| 商丘市| 丰城市| 乌拉特中旗| 辉县市| 尚志市| 肥城市| 广州市| 额尔古纳市| 长垣县| 高阳县| 托里县| 安乡县| 北票市| 宁陵县|