简体   繁体   English

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

[英]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.

相关问题 如何显示YUI分页器中的总页数? - How to show Total number of pages in YUI Paginator? 如何显示图表中的元素总数? - How can I show the total number of elements in ReCharts? 如何以编程方式在我的js控制器中设置jqx-grid的总页数? - How can I set the total pages number for the jqx-grid in my js controller programatically? VUEJS 组件将我的显示路由添加到他们的 get 调用中,我怎样才能让我的 journals/:id 页面工作。 哑剧错误 - VUEJS components adding my show route to their get call, how can I get my journals/:id pages to work. Mime Error 我怎样才能制作页面,每个页面包含10个帖子? - how can i make pages ,each one contains 10 posts? 如何获得 TypeScript 中数字的总位数? - How can I get a count of the total number of digits in a number in TypeScript? 如何使帖子的第一张图片与主页上的帖子标题一起显示,而不仅仅是作为文本链接的帖子标题? - How can I make the first image of posts appear with the titles of the posts on the home page, not just a post title as Text Link? vuejs-如何显示每个帖子的点赞次数 - vuejs - How to show the number of likes for each post 如何在主页出现 3 分钟后显示图像? - How can i show an image 3 minutes after the home page appears? jQuery仅在帖子上发生冲突,在页面和首页(wordpress)中不发生冲突 - jQuery conflict only on posts, not in pages and home (wordpress)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM