当前位置: 移动技术网 > IT编程>脚本编程>vue.js > VUE前端cookie简单操作

VUE前端cookie简单操作

2017年12月12日  | 移动技术网IT编程  | 我要评论

温建婷,倚天风云录,洗衣机排名 惠而浦

如下是简单cookie操作,当前仅限前端实例,具体内容如下

要注意的有两点:

1、cookie内容存贮的名称
2、删除cookie是通过设置过期为过去时间实现的

<body>
<div id="app">
 <button @click="clearcookie()">
 清除cookie
 </button>
</div>
</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>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网