本文主要为大家详细介绍了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插件实现换肤功能示例
jquery基于cookie实现换肤功能实例
以上就是vue前端cookie简单操作实例分享的详细内容。