实现的逻辑是:前端每调用成功一次页面数据接口,就相当于阅读了一次文章,前端获取到数据成功后,将访问量字段的值自增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
    );
  }

 

Logo

前往低代码交流专区

更多推荐