想要显示类似 1个月内,3天内,一年内 等所谓的相对时间, 可以采用 dayjs这个插件
dayjs的相对时间的使用方式,需要扩展一个自身的功能
1、 安装dayjs插件
$ npm i dayjs -S
$ yarn add dayjs
2、 封装一个相对时间函数
import dayjs from 'dayjs'
import 'dayjs/locale/zh-cn'
import relativeTime from 'dayjs/plugin/relativeTime'
dayjs.extend(relativeTime)
// 转化时间到相对时间
function relTime (date) {
return dayjs().locale('zh-cn').from(date)
}
3、注册成过滤器
export default {
install (Vue) {
Vue.prototype.$sleep = sleep
Vue.filter('relTime', relTime)
}
}
4、使用:
<span>{{ article.pubdate | relTime }}</span>