英文:
How can i use vuetify 3.3.4 date picker component in my project?
问题
我正在尝试使用最新的Vuetify 3(版本3.3.4 - lab)日期选择器组件。我在项目的组件文件夹中创建了一个日期输入组件。
/components/DateInput.vue
<template>
<div>
<v-menu
v-model="menu"
:close-on-content-click="false"
:nudge-right="40"
transition="scale-transition"
offset-y
min-width="290px"
>
<template v-slot:activator="{ props }">
<v-text-field
v-bind="props"
:value="dateFormatted"
variant="outlined"
append-inner-icon="mdi-calendar"
@change="onChange"
@input="updateDate"
></v-text-field>
</template>
<v-date-picker
:value="getDate"
@change="onChange"
@input="updateDate"
></v-date-picker>
</v-menu>
</div>
</template>
<script>
export default {
props: {
/**
* Date on ISO format to be edited.
* @model
*/
value: {
type: String,
default() {
return "";
},
},
},
data() {
return {
menu: false,
};
},
computed: {
dateFormatted() {
return this.input ? new Date(this.input) : "";
},
getDate() {
/**
* Return ISO 8601
*/
let date = this.input ? new Date(this.input) : new Date();
let month = 1 + date.getMonth();
if (month < 10) {
month = `0${month}`;
}
let day = date.getDate();
if (day < 10) {
day = `0${day}`;
}
return `${date.getFullYear()}-${month}-${day}`;
},
},
methods: {
onChange(val) {
console.error(val)
},
updateDate(val) {
this.menu = false;
console.error(val)
},
},
};
</script>
/App.vue
<template>
<v-container>
<v-row>
<v-col cols="6">
<date-input></date-input>
</v-col>
</v-row>
</v-container>
</template>
<script>
import DateInput from './components/DateInput.vue';
export default {
components: {
DateInput,
},
}
</script>
当我尝试选择日期时,我总是收到以下错误。
我做错了什么?我已经将项目部署到codesandbox,这是codesandbox链接。
https://codesandbox.io/p/github/eguvenc/test/main
英文:
I am trying to use latest vuetify 3 (3.3.4 version - lab) date-picker component.
I created a date-input component in my project component folder.
/components/DateInput.vue
<template>
<div>
<v-menu
v-model="menu"
:close-on-content-click="false"
:nudge-right="40"
transition="scale-transition"
offset-y
min-width="290px"
>
<template v-slot:activator="{ props }">
<v-text-field
v-bind="props"
:value="dateFormatted"
variant="outlined"
append-inner-icon="mdi-calendar"
@change="onChange"
@input="updateDate"
></v-text-field>
</template>
<v-date-picker
:value="getDate"
@change="onChange"
@input="updateDate"
></v-date-picker>
</v-menu>
</div>
</template>
<script>
export default {
props: {
/**
* Date on ISO format to be edited.
* @model
*/
value: {
type: String,
default() {
return ""
},
},
},
data() {
return {
menu: false,
};
},
computed: {
dateFormatted() {
return this.input ? new Date(this.input) : "";
},
getDate() {
/**
* Return ISO 8601
*/
let date = this.input ? new Date(this.input) : new Date();
let month = 1 + date.getMonth();
if (month < 10) {
month = `0${month}`;
}
let day = date.getDate();
if (day < 10) {
day = `0${day}`;
}
return `${date.getFullYear()}-${month}-${day}`;
},
},
methods: {
onChange(val) {
console.error(val)
},
updateDate(val) {
this.menu = false;
console.error(val)
},
},
};
</script>
/App.vue
<template>
<v-container>
<v-row>
<v-col cols="6">
<date-input></date-input>
</v-col>
</v-row>
</v-container>
</template>
<script>
import DateInput from './components/DateInput.vue';
export default {
components: {
DateInput,
},
}
</script>
When i try to select a date i always get an error like below.
Where am i doing wrong ?
I deployed the project to codesanbox, here is the codesanbox link.
答案1
得分: 1
这是关于VDatePicker组件的一个问题。它已经被修复,但除非你想切换到夜间构建版本,否则我认为你需要等待下一个版本发布。
然而,可以通过传递正确的:modelValue
属性来避免这个问题(你似乎在使用不存在的:value
属性)。请注意,:modelValue
必须是包含Date对象的数组。所以尝试类似这样的方式:
<v-date-picker
:modelValue="getDate"
@update:modelValue="updateDate"
></v-date-picker>
(注意不再有@change
事件,而在Vue 3中@input
是@update:modelValue
)。
并且让getDate
返回一个Date数组:
getDate() {
const date = this.input ? new Date(this.input) : new Date()
return [date]
}
英文:
That's a bug in the VDatePicker component. It's already fixed, but unless you want to switch to the nightly build, I think you have to wait for the next release.
However, the problem can be avoided by passing in a correct :modelValue
property (you seem to use a non-existent :value
property). Note that :modelValue
has to be an array containing Date objects. So try something like:
<v-date-picker
:modelValue="getDate"
@update:modelValue="updateDate"
></v-date-picker>
(note that there is no @change
event anymore, and @input
is @update:modelValue
in Vue 3).
and have getDate
return a Date array:
getDate() {
const date = this.input ? new Date(this.input) : new Date()
return [date]
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论