[英]How can i show total number of posts on the home pages in NodeJS and Vuejs
i want to show the total number of posts available on my web application but i don't know where and how to do this.I am using MongoDB,node js and vue js.我想显示我的 Web 应用程序上可用的帖子总数,但我不知道在哪里以及如何执行此操作。我正在使用 MongoDB、node js 和 vue js。 Here is the nodejs code
这是nodejs代码
posts.route("/").get(function(req, res) {
Post.find(function(err, posts) {
if (err) {
res.json(err);
} else {
res.json(posts);
}
});
});
VueJS Code VueJS 代码
export default {
data() {
return {
posts: [],
}
},
methods: {
like() {
title: 'You Just Liked This Post'
show: true
}
},
mounted() {
axios
.get('http://localhost:5000/posts')
.then(response => (this.posts = response.data))
}
}
As posts
is your array of posts once the response is received, you can display the number of posts using javascripts Array.length
property.由于
posts
是您收到回复后的帖子数组,您可以使用javascripts Array.length
属性显示帖子数。
In Vue you can render this directly in the HTML template using {{ posts.length }}
or if you want to loop through them and actually display the posts, use the v-for
directive在 Vue 中,您可以使用
{{ posts.length }}
直接在 HTML 模板中呈现它,或者如果您想遍历它们并实际显示帖子,请使用v-for
指令
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.