vue点赞功能代码

之前公司做评论,第一次用cookie做点赞。游客点赞,把数据提交到后台,同时存到cookie中,一天只能点一次
 
created() {
    // 首页获取cookie 有没有点赞过
    this.cookieInit('useids')
  },
methods: {
  cookieInit(name) {
      // 初始化cookie
      if (!this.getCookie(name)) {
        // 第一次检测有没有点赞cookie,没有添加一下
        this.setCookie(name, '')
      }
      let arr = this.getCookie(name).split(',');
      for (let i in arr) {
        for (let j in this.list) {
          if (arr[i] == this.list[j].id) {
            this.list[j].isdianzan = true;
          }
        }
      }
    },
    getCookie(name) {
      // 获取cookie
      return Cookies.get(name);
    },
    setCookie(name, value) {
      // 设置cookie
      Cookies.set(name, _.toString(value)); // 格式为 '1,2,3'
    },
    setcookieId(name, id) {
      // 添加点赞id
      let value = _.concat(this.getCookie(name), id);
      this.setCookie(name, value)
    },
}
原文地址:https://www.cnblogs.com/amang/p/9961525.html