如何在我的项目中使用Vuetify 3.3.4日期选择器组件?

huangapple go评论72阅读模式
英文:

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>

当我尝试选择日期时,我总是收到以下错误。

如何在我的项目中使用Vuetify 3.3.4日期选择器组件?

我做错了什么?我已经将项目部署到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

&lt;template&gt;
&lt;div&gt;
&lt;v-menu
v-model=&quot;menu&quot;
:close-on-content-click=&quot;false&quot;
:nudge-right=&quot;40&quot;
transition=&quot;scale-transition&quot;
offset-y
min-width=&quot;290px&quot;
&gt;
&lt;template v-slot:activator=&quot;{ props }&quot;&gt;
&lt;v-text-field
v-bind=&quot;props&quot;
:value=&quot;dateFormatted&quot;
variant=&quot;outlined&quot;
append-inner-icon=&quot;mdi-calendar&quot;
@change=&quot;onChange&quot;
@input=&quot;updateDate&quot;
&gt;&lt;/v-text-field&gt;
&lt;/template&gt;
&lt;v-date-picker
:value=&quot;getDate&quot;   
@change=&quot;onChange&quot;
@input=&quot;updateDate&quot;
&gt;&lt;/v-date-picker&gt;
&lt;/v-menu&gt;
&lt;/div&gt;
&lt;/template&gt;
&lt;script&gt;
export default {
props: {
/**
* Date on ISO format to be edited.
* @model
*/
value: {
type: String,
default() {
return &quot;&quot;
},
},
},
data() {
return {
menu: false,
};
},
computed: {
dateFormatted() {
return this.input ? new Date(this.input) : &quot;&quot;;
},
getDate() {
/**
* Return ISO 8601
*/
let date = this.input ? new Date(this.input) : new Date();
let month = 1 + date.getMonth();
if (month &lt; 10) {
month = `0${month}`;
}
let day = date.getDate();
if (day &lt; 10) {
day = `0${day}`;
}
return `${date.getFullYear()}-${month}-${day}`;
},
},
methods: {
onChange(val) {
console.error(val)
},
updateDate(val) {
this.menu = false;
console.error(val)
},
},
};
&lt;/script&gt;

/App.vue

&lt;template&gt;
&lt;v-container&gt;
&lt;v-row&gt;
&lt;v-col cols=&quot;6&quot;&gt;
&lt;date-input&gt;&lt;/date-input&gt;
&lt;/v-col&gt;  
&lt;/v-row&gt;
&lt;/v-container&gt;
&lt;/template&gt;
&lt;script&gt;
import DateInput from &#39;./components/DateInput.vue&#39;;
export default {
components: {
DateInput,
},
}
&lt;/script&gt;

When i try to select a date i always get an error like below.

如何在我的项目中使用Vuetify 3.3.4日期选择器组件?

Where am i doing wrong ?
I deployed the project to codesanbox, here is the codesanbox link.

https://codesandbox.io/p/github/eguvenc/test/main

答案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:

      &lt;v-date-picker
        :modelValue=&quot;getDate&quot;
        @update:modelValue=&quot;updateDate&quot;
      &gt;&lt;/v-date-picker&gt;

(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]
}

huangapple
  • 本文由 发表于 2023年6月18日 22:29:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76501037.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定