A timeago filter for Vue.
npm install vue-timeago> A timeago component Vue.js
``bash`
yarn add vue-timeagoor
npm i vue-timeago
CDN: UNPKG | jsDelivr (available as window.VueTimeago)
For usages on version 4, please check out this branch.
`js
import VueTimeago from 'vue-timeago'
Vue.use(VueTimeago, {
name: 'Timeago', // Component name, Timeago by defaultdate-fns
locale: 'en', // Default locale
// We use under the hood`
// So you can use all locales from it
locales: {
'zh-CN': require('date-fns/locale/zh_cn'),
ja: require('date-fns/locale/ja')
}
})
Then in your lovely component:
`vue
`
`js`
Vue.use(VueTimeago, pluginOptions)
- Type: { [localeName: string]: any }
An object of locales.
- Type: string
The default locale name.
- Type: (date, locale, converterOptions) => string
A converter that formats regular dates in xxx ago or in xxx style.
Check out our default converter which uses date-fns/distance_in_words_to_now under the hood.
- Type: Object
Provide an object which will be available as argument converterOptions in the converter we mentioned above.
Our default converter supports most options that date-fns/distance_in_words_to_now library supports, namely:
- includeSeconds: (default: false) distances less than a minute are more detailedtrue
- addSuffix: (default: ) result specifies if the second date is earlier or later than the first
- Type: Date string numbertrue
- Required:
The datetime to be formatted .
- Type: number booleanfalse
- Default:
The period to update the component, in seconds.
You can omit this prop or set it to 0 or false to disable auto-update.
When true it will be equivalent to 60.
Just like the locale option in the plugin options, but this could override the global one.
Just like the converter option in the plugin options, but this could override the global one.
Just like the converterOptions option in the plugin options, but this could override the global one.
`js`
Vue.use(VueTimeago, {
locale: 'en',
locales: {
'zh-CN': require('date-fns/locale/zh_cn')
}
})
In your components you can use this.$timeago.locale to access the global locale, in this case it's en, the component will get updated when you set it to another valid locale, e.g. this.$timeago.locale = 'zh-CN'.
The older version (700 bytes gzipped) is much smaller than the current version (2.8kB gzipped) that uses date-fns.
But the current version gives more precise result (and hopefully handles more edge cases), and we don't need to maintain a big list of locale messages because date-fns already did it for us.
`bashfor dev
yarn example
MIT © EGOIST