vue.js - 我试图在 v-bind:max 中使用变量设置默认值
问题描述
嗨,我有一个日期输入,我想添加一个 max 属性,因为我想允许用户可以选择该日期下的日期,所以我有我的 html 代码:
<div class="form-group">
<label for="exampleInputEmail1">Fecha de Recaudación</label>
<input type="date"
class="form-control"
id="exampleInputEmail1"
v-bind:max="datePickerOptions"
v-model="form.collection_date"
placeholder="Ingresa la fecha de la recaudación"
required>
</div>
我的 VueJs 代码:
export default {
created() {
this.getBranchOfficeList();
this.getDate();
},
methods: {
getDate() {
var d = new Date();
d.setDate(d.getDate()-5);
const ye = new Intl.DateTimeFormat('en', { year: 'numeric' }).format(d);
const mo = new Intl.DateTimeFormat('en', { month: 'numeric' }).format(d);
const da = new Intl.DateTimeFormat('en', { day: 'numeric' }).format(d);
if(ye < 10) {
var new_year = '0' + ye;
} else {
var new_year = ye;
}
if(mo < 10) {
var new_month = '0' + mo;
} else {
var new_month = mo;
}
if(da < 10) {
var new_day = '0' + da;
} else {
var new_day = da;
}
this.date_picker = new_year +'-'+ new_month + '-' + new_day;
}
},
data: function() {
return {
datePickerOptions: this.date_picker
}
},
computed: {
isDisabled() {
return true;
}
}
}
你怎么能看到 datePickerOptions 有一个来自 this.date_picker 的值,它来自方法 getDate() 但它不起作用,为什么?
谢谢。
解决方案
您可以使用计算属性,而不是将其作为方法。还要删除方法 getDate() 因为名称是保留的函数名称
datan() {
return {
date_picker: null,
form: { // I have defined the form object here since you are using it in the v-model
.....
}
}
},
computed: {
isDisabled() {
return true;
},
computeMaxDate() {
var d = new Date();
d.setDate(d.getDate()-5);
const ye = new Intl.DateTimeFormat('en', { year: 'numeric' }).format(d);
const mo = new Intl.DateTimeFormat('en', { month: 'numeric' }).format(d);
const da = new Intl.DateTimeFormat('en', { day: 'numeric' }).format(d);
if(ye < 10) {
var new_year = '0' + ye;
} else {
var new_year = ye;
}
if(mo < 10) {
var new_month = '0' + mo;
} else {
var new_month = mo;
}
if(da < 10) {
var new_day = '0' + da;
} else {
var new_day = da;
}
this.date_picker = new_year +'-'+ new_month + '-' + new_day;
return this.date_picker;
}
}
修改后的模板块是
<div class="form-group">
<label for="exampleInputEmail1">Fecha de Recaudación</label>
<input type="date"
class="form-control"
id="exampleInputEmail1"
:max="computeMaxDate"
v-model="form.collection_date"
placeholder="Ingresa la fecha de la recaudación"
required>
</div>
在计算属性中,我刚刚复制粘贴了您的逻辑......所以您需要确保存储在“date_picker”中的值具有以下格式:
YYYY-MM-DD // eg: 2021-01-19
如果它不是上述格式,那么您需要调试并修复它
推荐阅读
- java - 在 RxJava 中,是通过可观察的多线程发出事件吗?
- reactjs - react js中根据url显示组件
- soundcloud - Soundcloud API 在公开可用的轨道上返回 404 Not Found 错误
- java - Apache Artemis 发布者和订阅者授权
- powerbi - 使用天和小时的 Power BI 连续 x 轴
- java - ProgressDialog 只显示一次,然后不再工作
- python - 根据其他数据框列映射熊猫列中的类别值
- c# - ASP.NET Core MVC - 传递给视图的模型属性在 Razor 中显示为空
- perl - Libxml - 如果满足特定条件,则替换 xml 值
- javascript - 如何在js中向上滚动到另一个div内的div?