vue項(xiàng)目實(shí)現(xiàn)登陸注冊(cè)效果
本文實(shí)例為大家分享了vue項(xiàng)目實(shí)現(xiàn)登陸注冊(cè)效果的具體代碼,供大家參考,具體內(nèi)容如下
主要內(nèi)容
本章目標(biāo):vue+element-ui完成注冊(cè)以及登陸
1.效果展示
2.視圖頁(yè)面:views
注冊(cè)頁(yè)面效果實(shí)現(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:'名字沒(méi)寫',trigger:'blur'}, {min:1,max:5,message:'長(zhǎng)度在3到5位'} ], password:[ {required:true,message:'名字沒(méi)寫',trigger:'blur'}, {min:3,max:5,message:'長(zhǎng)度在3到5位'} ] } }; }, methods: { submitForm(formName){ this.$refs[formName].validate((valid)=>{ if(valid){//如果校檢通過(guò)向后端發(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:'名字沒(méi)寫',trigger:'blur'}, {min:1,max:5,message:'長(zhǎng)度在3到5位'} ], password:[ {required:true,message:'名字沒(méi)寫',trigger:'blur'}, {min:3,max:5,message:'長(zhǎng)度在3到5位'} ] } }; }, methods: { submitForm(formName){ this.$refs[formName].validate((valid)=>{ if(valid){//如果校檢通過(guò)向后端發(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:"首頁(yè)", component:Home }, { path:'/login', name:"login", title:"登錄頁(yè)", component:Login, meta:{ login:true } } ] }); router.beforeEach( async (to,from,next) => { 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)){//需要登錄 if(isLogin){//已經(jīng)登錄的,直接通過(guò) 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'){//未登錄,但是要去登錄頁(yè) next(); } if(!isLogin && to.name !== 'login'){//未登錄,去的也不是登錄頁(yè) next({name:'login'}); } }else{ next(); } }) export default router;
總結(jié)
今天的內(nèi)容就先到這里,因?yàn)檫€有一些沒(méi)完善所以不是很好,后面再繼續(xù)改進(jìn)!
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- vue實(shí)現(xiàn)登陸登出的實(shí)現(xiàn)示例
- Vue2.0 axios前后端登陸攔截器(實(shí)例講解)
- vue+axios新手實(shí)踐實(shí)現(xiàn)登陸的示例代碼
- 詳解用vue.js和laravel實(shí)現(xiàn)微信授權(quán)登陸
- Vue 前端實(shí)現(xiàn)登陸攔截及axios 攔截器的使用
- 基于vue-cli3和element實(shí)現(xiàn)登陸頁(yè)面
- Vue學(xué)習(xí)之路之登錄注冊(cè)實(shí)例代碼
- vue 實(shí)現(xiàn)通過(guò)手機(jī)發(fā)送短信驗(yàn)證碼注冊(cè)功能
- vue.js實(shí)現(xiàn)用戶評(píng)論、登錄、注冊(cè)、及修改信息功能
- VUE實(shí)現(xiàn)注冊(cè)與登錄效果
相關(guān)文章
前端實(shí)現(xiàn)pdf預(yù)覽功能的全過(guò)程(基于vue)
這篇文章主要給大家介紹了關(guān)于前端實(shí)現(xiàn)pdf預(yù)覽功能的相關(guān)資料,前端實(shí)現(xiàn)預(yù)覽最好的效果還是PDF,不會(huì)出現(xiàn)一些文字錯(cuò)亂和亂碼的問(wèn)題,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-09-09vue 修改 data 數(shù)據(jù)問(wèn)題并實(shí)時(shí)顯示的方法
今天小編就為大家分享一篇vue 修改 data 數(shù)據(jù)問(wèn)題并實(shí)時(shí)顯示的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-08-08vue項(xiàng)目記錄鎖定和解鎖功能實(shí)現(xiàn)
這篇文章主要為大家詳細(xì)介紹了vue項(xiàng)目記錄鎖定和解鎖功能實(shí)現(xiàn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03使用Vue調(diào)取接口,并渲染數(shù)據(jù)的示例代碼
今天小編就為大家分享一篇使用Vue調(diào)取接口,并渲染數(shù)據(jù)的示例代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-10-10簡(jiǎn)單聊一聊vue中data的代理和監(jiān)聽(tīng)
這篇文章主要給大家介紹了關(guān)于vue中data的代理和監(jiān)聽(tīng)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用vue具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-09-09Vue infinite update loop的問(wèn)題解決
這篇文章主要介紹了Vue "...infinite update loop..."的問(wèn)題解決,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-04-04python虛擬環(huán)境 virtualenv的簡(jiǎn)單使用
virtualenv是一個(gè)創(chuàng)建隔絕的Python環(huán)境的工具。這篇文章主要介紹了python虛擬環(huán)境 virtualenv的簡(jiǎn)單使用,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-01-01Vue中對(duì)watch的理解(關(guān)鍵是immediate和deep屬性)
watch偵聽(tīng)器,是Vue實(shí)例的一個(gè)屬性,是用來(lái)響應(yīng)數(shù)據(jù)的變化,需要在數(shù)據(jù)變化時(shí)執(zhí)行異步或開(kāi)銷較大的操作時(shí),這個(gè)方式是最有用的,這篇文章主要介紹了Vue中對(duì)watch的理解,需要的朋友可以參考下2022-11-11詳解Vue項(xiàng)目引入CreateJS的方法(親測(cè)可用)
CreateJS是基于HTML5開(kāi)發(fā)的一套模塊化的庫(kù)和工具。這篇文章主要介紹了Vue項(xiàng)目引入CreateJS的方法(親測(cè)),需要的朋友可以參考下2019-05-05