Last active
September 26, 2020 15:03
-
-
Save paulredmond/871f8eba7ae5d1eb3bc020dd9391ce17 to your computer and use it in GitHub Desktop.
Example Vue.js mixins for time
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import dateMixin from './mixins/date'; | |
// Globally | |
Vue.mixin(dateMixin); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import moment from 'moment'; | |
export default { | |
filters: { | |
toISOString(str) { | |
return moment(str).toISOString(); | |
}, | |
formatDate(str, format = null, outputFormat = 'YYYY-MM-DD HH:mm:ss') { | |
if (format == null) { | |
return moment(str).format(outputFormat); | |
} | |
return moment(str, format).format(outputFormat); | |
}, | |
diffForHumans(str) { | |
return moment(str).from(moment()); | |
}, | |
}, | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<template> | |
<div> | |
<p>{{ myDate | toISOString }}</p> | |
<p>{{ myDate | formatDate(null, 'YYYY-MM-DD') }} | |
<p>{{ myDate | diffForHumans }}</p> | |
</div> | |
</template> | |
<script> | |
// Per-component example | |
import dateMixin from '../mixins/date'; | |
export default { | |
mixins: [dateMixin] | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To create filters, you dont need make a mixin.
Just use this
https://br.vuejs.org/v2/guide/filters.html
But this help me in anothers problems.