本文主要為大家詳細介紹了VUE前端cookie簡單操作程式碼,具有一定的參考價值,有興趣的小夥伴們可以參考一下,希望能幫助大家。
如下是簡單cookie操作,目前僅限前端實例,具體內容如下
要注意的有兩點:
1、cookie內容存貯的名稱
2、刪除cookie是透過設定過期為過去時間實現的
<body> <p id="app"> <button @click="clearCookie()"> 清除cookie </button> </p> </body> <script> let app = new Vue({ el: "#app", data: { }, created: function () { this.checkCookie(); }, methods: { //设置cookie setCookie: function (cname, cvalue, exdays) { var d = new Date(); d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000)); var expires = "expires=" + d.toUTCString(); console.info(cname + "=" + cvalue + "; " + expires); document.cookie = cname + "=" + cvalue + "; " + expires; console.info(document.cookie); }, //获取cookie getCookie: function (cname) { var name = cname + "="; var ca = document.cookie.split(';'); for (var i = 0; i < ca.length; i++) { var c = ca[i]; while (c.charAt(0) == ' ') c = c.substring(1); if (c.indexOf(name) != -1) return c.substring(name.length, c.length); } return ""; }, //清除cookie clearCookie: function () { this.setCookie("username", "", -1); }, checkCookie: function () { var user = this.getCookie("username"); if (user != "") { alert("Welcome again " + user); } else { user = prompt("Please enter your name:", ""); if (user != "" && user != null) { this.setCookie("username", user, 365); } } } } }) </script>
相關推薦:
# #HTML5 Web快取與運用程式快取(cookie,session)
jQuery結合jQuery.cookie.js外掛實現換膚功能範例
以上是VUE前端cookie簡單操作實例分享的詳細內容。更多資訊請關注PHP中文網其他相關文章!