javascript - 我想使用 Javascript/Vue 将当前周作为日期数组返回
解决方案
在此示例中使用计算属性很重要,因为您可以更改周(下周或之前)。
尝试这样的事情:
<template>
<div>
<div>
<button @click="priorWeek">Prior Week</button>
<span v-for="day in days" class="day">
{{ day.toLocaleDateString() }}
</span>
<button @click="nextWeek">Next Week</button>
</div>
</div>
</template>
<script>
export default {
name: 'App',
data: () => ({
first: new Date(),
}),
computed: {
days() {
return [
this.first,
this.addDays(this.first, 1),
this.addDays(this.first, 2),
this.addDays(this.first, 3),
this.addDays(this.first, 4),
this.addDays(this.first, 5),
this.addDays(this.first, 6),
];
},
},
methods: {
addDays(date, days) {
let newDate = new Date(date);
newDate.setDate(this.first.getDate() + days);
return newDate;
},
subtractDays(date, days) {
let newDate = new Date(date);
newDate.setDate(this.first.getDate() - days);
return newDate;
},
nextWeek() {
this.first = this.addDays(this.first, 7);
},
priorWeek() {
this.first = this.subtractDays(this.first, 7);
},
},
};
</script>
<style>
.day {
background-color: rgb(240, 240, 240);
border-radius: 1rem;
color: purple;
display: inline-block;
margin-left: 0.5rem;
padding: 0.5rem;
}
</style>
推荐阅读
- javascript - 如何使预取链接与 JavaScript 文件一起使用?
- visual-studio-code - 源代码管理 VSCode 中的事件处理
- python - 卸载 distutils 安装项目失败
- sql - 从列类型开始,如何在 Postgres 中找到支持的聚合?
- mongodb - Mongo脚本不修改集合
- bash - 在 NCO 中附加/组合数千 (42000) 个 netCDF 文件的更快方法
- .net-core-3.0 - C# Marshal 字节数组到结构
- python-3.x - mypy可以根据当前对象的类型选择方法返回类型吗?
- javascript - 为什么 Ngrx 8.2 效果会引发类型不匹配错误
- java - GetTypeMap 为 LinkedHashMap 到 DTO 映射返回 Null