Javascript 如何在NodeJS和Vuejs中显示主页上的帖子总数
我想显示我的web应用程序上可用的帖子总数,但我不知道在哪里以及如何做。我正在使用MongoDB、node js和vue js。 下面是nodejs代码Javascript 如何在NodeJS和Vuejs中显示主页上的帖子总数,javascript,node.js,mongodb,vue.js,vue-cli,Javascript,Node.js,Mongodb,Vue.js,Vue Cli,我想显示我的web应用程序上可用的帖子总数,但我不知道在哪里以及如何做。我正在使用MongoDB、node js和vue js。 下面是nodejs代码 posts.route("/").get(function(req, res) { Post.find(function(err, posts) { if (err) { res.json(err); } else { res.json(posts); } }); }); VueJS代码
posts.route("/").get(function(req, res) {
Post.find(function(err, posts) {
if (err) {
res.json(err);
} else {
res.json(posts);
}
});
});
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))
}
}
由于
posts
是收到响应后的帖子数组,因此可以使用javascriptsarray.length
属性显示帖子数量
在Vue中,您可以使用{{{posts.length}
直接在HTML模板中呈现这些内容,或者如果您希望循环浏览这些内容并实际显示这些内容,请使用v-for
指令