實現的邏輯是:前端每調用成功一次頁面數據接口,就相當於閱讀了一次文章,前端獲取到數據成功後,將訪問量字段的值自增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
);
}