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

前端vue+elementUI如何實(shí)現(xiàn)記住密碼功能

 更新時(shí)間:2020年09月20日 09:15:26   作者:劉亞津  
這篇文章主要給大家介紹了關(guān)于vue+elementUI如何實(shí)現(xiàn)記住密碼功能的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

我們這回使用純前端保存密碼

既然是記住密碼,前端也就是使用cookie保存,訪問時(shí)用cookie讀取

先來了解下cookie的基本使用吧

Cookie

所有的cookie信息都在document.cookie中存放著,是一個(gè)字符串,里面的cookie以分號(hào)和空格分隔。就像這樣:

	"key1=value1; key2=value2; key3=value3"
	// 使用document.cookie 來獲取所有cookie
	docuemnt.cookie = "id="+value

操作cookie

	/**
 * 設(shè)置cookie值
 *
 * @param {String} name cookie名稱
 * @param {String} value cookie值
 * @param {Number} expiredays 過期時(shí)間,天數(shù)
 */
 function setCookie (name, value, expiredays) {
 let exdate = new Date() 
 		//setDate獲取N天后的日期
 exdate.setDate(exdate.getDate() + expiredays) //getDate() 獲取當(dāng)前月份的日 + 過期天數(shù)
 document.cookie =name+"="+encodeURI(value)+";path=/;expires="+exdate.toLocaleString()
 }
 /**
 * 獲取cookie值
 * @param {String} name cookie名稱
 */
 function getCookie (name) {
  var arr = document.cookie.split(";") //轉(zhuǎn)換數(shù)組
  //["_ga=GA1.1.1756734561.1561034020", " mobile=123" password=456"]
  for(var i=0;i<arr.length;i++){
   var arr2 = arr[i].split('='); // ["_ga", "GA1.1.1756734561.1561034020"]
   if(arr2[0].trim() == name){
    return arr2[1]
   }
  }
 }
 /**
 * 刪除指定cookie值
 * @param {String} name cookie名稱
 */
 function clearCookie (name) {
 setCookie(name, '', -1)
 }
 /**
 * 瀏覽器是否支持本地cookie
 */
 function isSupportLocalCookie () {
 let {name, value} = {name: 'name', value: 'mingze'}
 setCookie(name, value, 1) //設(shè)置cookie
 return getCookie(name).includes(value) //includes 判斷數(shù)組name中是否含有當(dāng)前value(返回true or false)
 }

好了了解了cookie我們開始如何實(shí)現(xiàn)一個(gè)簡單的記住密碼功能

HTML代碼

 <el-form :model="ruleForm" :rules="rules"
 status-icon
 ref="ruleForm" 
 label-position="left" 
 label-width="0px" 
 class="demo-ruleForm login-page">
 <h3 class="title">系統(tǒng)登錄</h3>
<el-form-item prop="username">
  <el-input type="text" 
   v-model="ruleForm2.username" 
   auto-complete="off" 
   placeholder="用戶名"
  ></el-input>
 </el-form-item>
<el-form-item prop="password">
  <el-input type="password" 
   v-model="ruleForm2.password" 
   auto-complete="off" 
   placeholder="密碼"
  ></el-input>
 </el-form-item>
<el-checkbox v-model="checked" style="color:#a0a0a0;margin:0 0 20px 0">記住密碼</el-checkbox>
<el-form-item style="width:100%;">
 <el-button type="primary" style="width:100%;" @click="handleSubmit" :loading="logining">登錄	</el-button>
</el-form-item>
</el-form>

vue代碼

 data(){
  return {
  	 logining: false,
   checked: true
   ruleForm: {
    username: '',
    password: '',
   },
   rules: {
    username: [{required: true, message: '請(qǐng)輸入您的帳戶', trigger: 'blur'}],
    password: [{required: true, message: '請(qǐng)輸入您的密碼', trigger: 'blur'}]
   },
  }
 },
 mounted() {
  this.account() //獲取cookie的方法
 },
 account(){
  if(document.cookie.length <= 0){
   console.log(this.getCookie('mobile'))
   this.ruleForm.username = this.getCookie('mobile')
   this.ruleForm.password = this.getCookie('password')
  }
 },
 setCookie(c_name,c_pwd,exdate){ //賬號(hào),密碼 ,過期的天數(shù)
  var exdate = new Date()
  console.log(c_name,c_pwd)
  exdate.setTime(exdate.getTime() + 24 * 60 * 60 * 1000 * exdate) //保存的天數(shù)
  document.cookie ="mobile="+c_name+";path=/;expires="+exdate.toLocaleString()
  document.cookie ="password="+c_pwd+";path=/;expires="+exdate.toLocaleString()
 },
 getCookie(name){
  var arr = document.cookie.split(";") 
   //["_ga=GA1.1.1756734561.1561034020", " mobile=123" password=456"]
   for(var i=0;i<arr.length;i++){
    var arr2 = arr[i].split('='); // ["_ga", "GA1.1.1756734561.1561034020"]
    if(arr2[0].trim() == name){
     return arr2[1]
    }
   }
  },
  clearCookie(){
   this.setCookie("","",-1) //清除cookie 
  },
	handleSubmit(){ //提交登錄
	 this.$refs.ruleForm.validate((valid) => {
   if(valid){
   	this.logining = true;
   	var _this = this;
   	if(_this.checked == true){
   	 	//存入cookie
    _this.setCookie(_this.ruleForm.username,_this.ruleForm.password,7) //保存7天
   }else{
    _this.clearCookie();
   }
   Login({
    mobile:_this.ruleForm.username,
    password:_this.ruleForm.password
   }).then((result) => {
    console.log(result)
    _this.$alert('登陸成功')
   })
  }
	}

好了,這回的記住密碼就到這里,小伙伴們一起努力吧 ^ 0 ^

總結(jié)

到此這篇關(guān)于前端vue+elementUI如何實(shí)現(xiàn)記住密碼功能的文章就介紹到這了,更多相關(guān)vue+elementUI記住密碼內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • vue二次封裝一個(gè)高頻可復(fù)用組件的全過程

    vue二次封裝一個(gè)高頻可復(fù)用組件的全過程

    在開發(fā)Vue項(xiàng)目我們一般使用第三方UI組件庫進(jìn)行開發(fā),但是這些組件提供的接口并不一定滿足我們的需求,這時(shí)我們可以通過對(duì)組件庫組件的二次封裝,來滿足我們特殊的需求,這篇文章主要給大家介紹了關(guān)于vue二次封裝一個(gè)高頻可復(fù)用組件的相關(guān)資料,需要的朋友可以參考下
    2022-10-10
  • vue更多篩選項(xiàng)小組件使用詳解

    vue更多篩選項(xiàng)小組件使用詳解

    這篇文章主要為大家詳細(xì)介紹了vue更多篩選項(xiàng)小組件的使用方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-06-06
  • vue的style綁定background-image的方式和其他變量數(shù)據(jù)的區(qū)別詳解

    vue的style綁定background-image的方式和其他變量數(shù)據(jù)的區(qū)別詳解

    今天小編就為大家分享一篇vue的style綁定background-image的方式和其他變量數(shù)據(jù)的區(qū)別詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-09-09
  • Vue實(shí)現(xiàn)父子組件頁面刷新的幾種常用方法

    Vue實(shí)現(xiàn)父子組件頁面刷新的幾種常用方法

    很多時(shí)候我們?cè)诓僮鬟^頁面時(shí)候,特別是增刪改操作之后,數(shù)據(jù)會(huì)有所改變,本文主要介紹了Vue實(shí)現(xiàn)父子組件頁面刷新的幾種常用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-04-04
  • Vue項(xiàng)目本地沒有問題但部署到服務(wù)器上提示錯(cuò)誤(問題解決方案)

    Vue項(xiàng)目本地沒有問題但部署到服務(wù)器上提示錯(cuò)誤(問題解決方案)

    一個(gè) VUE 的項(xiàng)目在本地部署沒有問題,但是部署到服務(wù)器上的時(shí)候提示訪問資源的錯(cuò)誤,遇到這樣的問題如何解決呢?下面小編給大家?guī)砹薞ue項(xiàng)目本地沒有問題但部署到服務(wù)器上提示錯(cuò)誤的解決方法,感興趣的朋友一起看看吧
    2023-05-05
  • 簡單談一談Vue中render函數(shù)

    簡單談一談Vue中render函數(shù)

    vue中的render函數(shù),它返回的是一個(gè)虛擬節(jié)點(diǎn)vnode,也就是我們要渲染的節(jié)點(diǎn),下面這篇文章主要給大家介紹了關(guān)于Vue中render函數(shù)的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-09-09
  • vue中的封裝常用工具類

    vue中的封裝常用工具類

    這篇文章主要介紹了vue中的封裝常用工具類,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-05-05
  • Vue.js雙向綁定實(shí)現(xiàn)原理詳解

    Vue.js雙向綁定實(shí)現(xiàn)原理詳解

    這篇文章主要為大家詳細(xì)介紹了Vue.js雙向綁定實(shí)現(xiàn)原理,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-12-12
  • vue3中使用百度地圖的簡單步驟

    vue3中使用百度地圖的簡單步驟

    最近項(xiàng)目要用到百度地圖api,好久沒用到地圖,就百度了一番,下面這篇文章主要給大家介紹了關(guān)于vue3中使用百度地圖的簡單步驟,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-06-06
  • Vue header組件開發(fā)詳解

    Vue header組件開發(fā)詳解

    本篇文章主要介紹了Vue header組件開發(fā)詳解,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-01-01

最新評(píng)論