Reputation: 891
I created a countdown with Vue.js but I am having trouble showing the values I am getting. I have two components and I have read the single file component guide from Vue but I just don't seem to understand what it is that I am doing wrong. In the console I get the follow error:
[Vue warn]: Invalid prop: type check failed for prop "date". Expected Number, got String.
Though in my code it is defined as a number.
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>
Upvotes: 21
Views: 136471
Reputation: 73609
As the error is saying, It is coming from this line:
<Countdown date="March 20, 2017 12:00"></Countdown>
You are passing date
as String, while in props there is validation for it being an Number. Here is your validation:
props: {
date: {
type: Number,
coerce: str => Math.trunc(Date.parse(str) / 1000)
},
},
I think in the new project you are using vuejs2, where coerce option has been removed. As stated here you can use a computed property like following:.
props: {
date: {
type: Number
},
},
computed: {
modifiedDate: function(){
return Math.trunc(Date.parse(this.date) / 1000)
}
}
You can use the modifiedDate
now instead of date
.
Upvotes: 16
Reputation: 13644
It is nothing wrong with Vue. The issue is in your code.
You declared date as number (btw. why?) and then you are passing string...
Declaration:
props: {
date: {
type: Number,
coerce: str => Math.trunc(Date.parse(str) / 1000)
},
},
Usage:
<Countdown date="March 20, 2017 12:00"></Countdown>
Using number to store date is not best idea (it can work but there are better ways).
Upvotes: 1