我如何在 NodeJS 和 Vuejs 的主页上显示帖子总数

How can i show total number of posts on the home pages in NodeJS and Vuejs

我想显示我的 Web 应用程序上可用的帖子总数,但我不知道在哪里以及如何做 this.I 我正在使用 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 代码

  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 是收到响应后的帖子数组,您可以使用 javascripts Array.length 属性.

显示帖子数

在 Vue 中,您可以使用 {{ posts.length }} 直接在 HTML 模板中呈现它,或者如果您想遍历它们并实际显示帖子,请使用 v-for 指令