[Vue警告]的原因是什么:无效的道具:道具“值”的自定义验证程序检查失败

时间:2019-05-15 18:23:51

标签: vue.js nuxt.js vuejs-datepicker

我有nuxt.js应用,该应用使用vuejs-datepicker

<template>
<!-- ... -->

    <DatePicker :value="datePicker.value" />
 <!-- ... -->
 </template>

和一系列日期变量:

<script>
import DatePicker from 'vuejs-datepicker'
import { DateTime } from 'luxon'

const moment = require('moment')

const date = new Date(2016, 8, 16)
const date2 = moment('2016-09-16').toDate()
const date3 = DateTime.local(2016, 9, 16).toJSDate()

export default {
  components: {
    DatePicker
  },
  data() {
    return {
      datePicker: {
        mondayFirst: true,
        format: 'dd.MM.yyyy',
        value: date
      }
    }
  }
}

当我将其“值”属性绑定到通常的日期变量“ date”时,一切正常,但是当我选择date2或date3时,会收到此烦人的警告

[Vue warn]: Invalid prop: custom validator check failed for prop "value".

found in

---> <DatePicker>
       <StaticPositionsTab> at components/StaticPositions.vue
         <BTab>
           <BTabs>
             <Pages/index.vue> at pages/index.vue
               <Nuxt>
                 <Layouts/default.vue> at layouts/default.vue
                   <Root>

我发现了用于value属性的自定义验证器,它非常简单明了,在所有三种情况下都返回true:

value: {
      validator: function (val) { return utils$1.validateDateInput(val); }
    }

...

validateDateInput (val) {
    return val === null || val instanceof Date || typeof val === 'string' || typeof val === 'number'
  }

但是有什么区别呢?可能是Vue.js错误本身吗?

1 个答案:

答案 0 :(得分:1)

date2date3是对象,而value需要为null |日期|字符串数字

const date2 = moment('2016-09-16').toDate()
const date3 = DateTime.local(2016, 9, 16).toJSDate()

typeof date2 // object
typeof date3 // object

您应使用.toString()将其转换为字符串

const date2 = moment('2016-09-16').toString()
const date2 = moment('2016-09-16').toISOString()
const date3 = DateTime.local(2016, 9, 16).toString()

或使用Unix时间戳

const date2 = moment('2016-09-16').unix()
const date3 = moment('2016-09-16').valueOf()
const date4 = moment('2016-09-16').getTime()
const date5 = DateTime.local(2016, 9, 16).valueOf()