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
    );
  }

 

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