vue如何实现点击一篇文章后,文章的访问量增加1个

实现的逻辑是:前端每调用成功一次页面数据接口,就相当于阅读了一次文章,前端获取到数据成功后,将访问量字段的值自增1个,然后将增加后访问量字段再传给后端,保存在数据库中

代码实现:

 getArticle(index, types, idIndex) {
      this.axios
        .get("articles/get")
        .then(res => {
          this.articleObj = res.data.resulet;
          //阅读量自增
          this.articleObj.Pageview = res.data.resulet.Pageview + 1;
          //提交增加后的阅读量数据
          this.submit();
        })
        .catch(err => {
          console.log("err", err);
        });
    },
    // 增加阅读量
    submit() {
      let article = {};
      article[this.articleObj.types] = [this.articleObj];
      this.axios
        .post("http://localhost:3001/articles/post", {
          data: {
            userName: "longwei",
            types: this.articleObj.types,
            id: this.articleObj.id,
            article: article
          }
        })
       
    }
  },
  mounted() {
    this.getArticle(
      this.$route.query.articleIndex,
      this.$route.query.types,
      this.$route.query.idIndex
    );
  }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章