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

Auz*_*uzy 7 javascript vue.js vuejs2

我通过 v-for 遍历日历中的日期:

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

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

eVeNtKeY
  date: 12/7/17
Run Code Online (Sandbox Code Playgroud)

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

data: {
  thisMonthsEvents: [bunch of firebase data],
  currentMonth: number
}
Run Code Online (Sandbox Code Playgroud)

有没有办法为每次重复运行一个方法v-for?在我看来,我可以这样做:

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

tha*_*ksd 9

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

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

<li v-for="day in daysInMonth">
  {{ getEventsByDay(day) }}
</li>
Run Code Online (Sandbox Code Playgroud)