Vue - 每个v-for项目的运行方法

时间:2017-07-05 20:05:08

标签: javascript vue.js vuejs2

我通过v-for:

在日历中循环了几天

<ul><li v-for="day in daysInMonth"></li></ul>

在Firebase中,我存储了事件。每个事件都有一个日期属性:

eVeNtKeY
  date: 12/7/17

现在,每天都没有活动,但我需要每天获得一个活动计数。我通过Firebase调用下载了本月的所有活动,并将其作为数组data保存在Vue thisMonthsEvent中。

data: {
  thisMonthsEvents: [bunch of firebase data],
  currentMonth: number
}

有没有办法为每次重复v-for运行一个方法?在我的想法中,我可以做这样的事情:

    computed: {
        eventsByDay () {
          return this.thisMonthsEvents.filter(function (event) {
            return event.date === this.currentMonth + '-' + Somehow get the day from loop + '-2017'
});
        }
      }

1 个答案:

答案 0 :(得分:2)

使用方法而不是计算属性并直接在v-for循环中调用方法,传入day变量:

methods: {
  getEventsByDay(day) {
    return this.thisMonthsEvents.filter(function (event) {
      return event.date === this.currentMonth + '-' + day + '-2017'
    });
  }
}

<li v-for="day in daysInMonth">
  {{ getEventsByDay(day) }}
</li>