vue項目實現記住密碼到cookie功能樣本(附源碼),vuecookie

來源:互聯網
上載者:User

vue項目實現記住密碼到cookie功能樣本(附源碼),vuecookie

本文介紹了vue項目實現記住密碼到cookie功能樣本,分享給大家,具體如下:

登陸頁面

實現功能:

1.記住密碼勾選,點登陸時,將帳號和密碼儲存到cookie,下次登陸自動顯示到表單內
2.不勾選,點登陸時候則清空之前儲存到cookie的值,下次登陸需要手動輸入

大體思路就是通過存/取/刪cookie實現的;每次進入登入頁,先去讀取cookie,如果瀏覽器的cookie中有帳號資訊,就自動填滿到登入框中,存cookie是在登入成功之後,判斷目前使用者是否勾選了記住密碼,如果勾選了,則把帳號資訊存到cookie當中,如上:

直接上主要的代碼

HTML部分

<div class="ms-login">    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="0px" class="demo-ruleForm">      <el-form-item prop="username">        <el-input v-model="ruleForm.username" placeholder="使用者名稱"></el-input>      </el-form-item>      <el-form-item prop="password">        <el-input type="password" placeholder="密碼" v-model="ruleForm.password" @keyup.enter.native="submitForm('ruleForm')"></el-input>      </el-form-item>      <!-- `checked` 為 true 或 false -->      <el-checkbox v-model="checked">記住密碼</el-checkbox>      <br>      <br>      <div class="login-btn">        <el-button type="primary" @click="submitForm('ruleForm')">登入</el-button>      </div>    </el-form>  </div>

JS部分

  //頁面載入調用擷取cookie值  mounted() {    this.getCookie();  },  methods: {    submitForm(formName) {             const self = this;            //判斷複選框是否被勾選 勾選則調用配置cookie方法            if (self.checked == true) {              console.log("checked == true");              //傳入帳號名,密碼,和儲存天數3個參數              self.setCookie(self.ruleForm.username, self.ruleForm.password, 7);            }else {             console.log("清空Cookie");             //清空Cookie             self.clearCookie();            }                        //與後端請求代碼,本功能不需要與後台互動所以省略                        console.log("登陸成功");                 });      },      //設定cookie      setCookie(c_name, c_pwd, exdays) {        var exdate = new Date(); //擷取時間        exdate.setTime(exdate.getTime() + 24 * 60 * 60 * 1000 * exdays); //儲存的天數        //字串拼接cookie        window.document.cookie = "userName" + "=" + c_name + ";path=/;expires=" + exdate.toGMTString();        window.document.cookie = "userPwd" + "=" + c_pwd + ";path=/;expires=" + exdate.toGMTString();      },      //讀取cookie      getCookie: function() {        if (document.cookie.length > 0) {          var arr = document.cookie.split('; '); //這裡顯示的格式需要切割一下自己可輸出看下          for (var i = 0; i < arr.length; i++) {            var arr2 = arr[i].split('='); //再次切割            //判斷尋找相對應的值            if (arr2[0] == 'userName') {              this.ruleForm.username = arr2[1]; //儲存到儲存資料的地方            } else if (arr2[0] == 'userPwd') {              this.ruleForm.password = arr2[1];            }          }        }      },      //清除cookie      clearCookie: function() {        this.setCookie("", "", -1); //修改2值都為空白,天數為負1天就好了      }

瀏覽器中的cookie資訊如,注意這裡的cookie的expire/Max-Age到期時間,這個時間是格林尼治標準時間GMT,世界統一的時間,GMT+8小時就是北京時間。(這裡不做加密功能)


源碼連結 vue項目實現表單登入頁儲存帳號和密碼到cookie功能

以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支援幫客之家。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.