【问题标题】:Invalid prop: type check failed for prop无效的道具:道具的类型检查失败
【发布时间】:2017-07-31 14:33:21
【问题描述】:

我使用 Vue.js 创建了倒计时,但无法显示我得到的值。我有两个组件,我已经阅读了 Vue 的单文件组件指南,但我似乎不明白我做错了什么。在控制台中,我收到以下错误:

[Vue 警告]:无效的道具:道具“日期”的类型检查失败。期望的数字,得到字符串。

虽然在我的代码中它被定义为一个数字。

app.js

import './bootstrap.js';

import Echo from 'laravel-echo';
import Vue from 'vue';

import CurrentTime from './components/CurrentTime';
import Bitbucket from './components/Bitbucket';
import InternetConnection from './components/InternetConnection';
import LastFm from './components/LastFm';
import PackagistStatistics from './components/PackagistStatistics';
import RainForecast from './components/RainForecast';
import Placeholder from './components/Placeholder';
import Youtube from './components/Youtube';
import ProjectCountdown from './components/ProjectCountdown';
import Countdown from './components/Countdown';


Vue.component('countdown', Countdown);

new Vue({

el: '#dashboard',

components: {
    CurrentTime,
    InternetConnection,
    Bitbucket,
    LastFm,
    PackagistStatistics,
    RainForecast,
    Placeholder,
    Youtube,
    ProjectCountdown,
    Countdown
},

created() {
    this.echo = new Echo({
        broadcaster: 'pusher',
        key: window.dashboard.pusherKey,
        cluster: 'eu',
        encrypted: true
    });
},
});

ProjectCountdown.vue

<template>
<div id="dashboard">
    <Countdown date="March 20, 2017 12:00"></Countdown>
    {{days}}
</div>
</template>

<script>
import Grid from './Grid';
import Vue from 'vue';
import Countdown from './Countdown';

export default {

components: {
    Grid,
    Countdown,
},

props: {
    grid: {
        type: String,
    },
},

data() {
    return {

    }
}
}



// Vue.filter('two_digits', function (value) {
//     if(value.toString().length <= 1)
//     {
//         return "0" + value.toString()
//     }
//     return value.toString();
// });
</script>

Countdown.vue

<template>
<div>
    {{ seconds }}
</div>
</template>


<script>
import Vue from 'vue';
export default {

props: {
    date: {
        type: Number,
        coerce: str => Math.trunc(Date.parse(str) / 1000)
    },
},
data() {
    return {
        now: Math.trunc((new Date()).getTime() / 1000)
    }
},
ready() {
    window.setInterval(() => {
        this.now = Math.trunc((new Date()).getTime() / 1000);
    },1000);


},
computed: {
    seconds() {
        return (this.date - this.now) % 60;
    },
    minutes() {
        return Math.trunc((this.date - this.now) / 60) % 60;
    },
    hours() {
        return Math.trunc((this.date - this.now) / 60 / 60) % 24;
    },
    days() {
        return Math.trunc((this.date - this.now) / 60 / 60 / 24);
    },
},
}
</script>

【问题讨论】:

    标签: javascript vue.js vuejs2 vue-component


    【解决方案1】:

    正如错误所说,它来自这一行:

    <Countdown date="March 20, 2017 12:00"></Countdown>
    

    您将date 作为字符串传递,而在道具中验证它是一个数字。这是您的验证:

    props: {
        date: {
            type: Number,
            coerce: str => Math.trunc(Date.parse(str) / 1000)
        },
    },
    

    我认为在新项目中您使用的是 vuejs2,其中强制选项为 removed。如here 所述,您可以使用如下计算属性:。

    props: {
        date: {
            type: Number
        },
    },
    computed: {
       modifiedDate: function(){
           return Math.trunc(Date.parse(this.date) / 1000)
       }
    }
    

    您现在可以使用modifiedDate 代替date

    【讨论】:

    • 我知道,但是在我的另一个项目中,当导入 Vue.js 库并拥有一个名为 vue.js 的文件时,我做了完全相同的事情,它在那里工作,这就是我不明白的原因为什么它不能在这里使用完全相同的代码。
    • 我现在需要更改其他计算方法吗?
    • 不,你只是添加这个新方法,除非你在那里使用date变量,否则其他人不会受到影响,如果是的话,你可能会看到你是否应该在那里使用modifiedDate
    • 正如您在我的计算属性中看到的那样,我在所有函数中都使用了日期变量,例如:seconds() { return (this.date - this.now) % 60; },
    • 您可以在那些方法/计算属性中开始使用this,modifiedDate
    【解决方案2】:

    Vue 没有错。问题出在您的代码中。

    您将日期声明为数字(顺便说一句。为什么?)然后您正在传递字符串...

    声明:

    props: {
        date: {
            type: Number,
            coerce: str => Math.trunc(Date.parse(str) / 1000)
        },
    },
    

    用法: &lt;Countdown date="March 20, 2017 12:00"&gt;&lt;/Countdown&gt;

    使用数字来存储日期不是最好的主意(它可以工作,但有更好的方法)。

    【讨论】:

    猜你喜欢
    • 2017-07-11
    • 2021-09-15
    • 2021-07-27
    • 1970-01-01
    • 1970-01-01
    • 2019-02-07
    • 2019-05-17
    • 1970-01-01
    • 2021-06-18
    相关资源
    最近更新 更多