【发布时间】:2022-01-13 18:46:59
【问题描述】:
基本上我有这个日历行,每个元素都有日期信息:
我正在尝试停止单个数据的循环,以便每个日历项都有自己的关于当前和未来几天的数据。
第一个日历元素应始终显示当前日期,其余项目为剩余天数
模板
<template>
<div class="wrapper">
<div class="calendar-wrapper">
<div class="calendar-row"
v-for="(day, idx) in dayArr"
:key="idx"
>
<div
id="card"
class="day-card unactive"
@click="test(idx)"
>
<div class="card-info">
<p class="name">{{ dayOfWeek }}</p>
<p class="day">
{{ currDay }}
</p>
</div>
<div class="dot-wrapper">
<div class="dot-status undone" />
<div class="dot-status undone" />
</div>
</div>
</div>
</div>
</div>
</template>
脚本
setup() {
const moment = require('moment')
const m = moment
// Get the remaining month days starting from today:
const daysRemainingThisMonth = moment().endOf('month').diff(moment(), 'days');
// Turn the remaining days into an array for future rendering in the Template
let dayArr = Array(daysRemainingThisMonth)
// Get the index of a clicked calendar item:
const test = (idx) => {
console.log(idx + 1);
}
// Get the current day:
const currDay = m().format('D')
// Get the current week day:
const dayOfWeek = m().format('dddd')
}
我相信有一种方法可以以某种方式访问每个日历元素并为其提供自己的数据,但我不知道如何
【问题讨论】:
-
可能跑题了,但建议不要在新项目中再使用 momentJs,more info here。你可以试试Dayjs,所有的api都基本一样。
标签: javascript arrays vue.js momentjs