newPost.vue 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. <script setup lang="ts">
  2. import { ref } from 'vue'
  3. import service from '../../plugins/axios'
  4. //格式化时间几天前
  5. function formatDate(date: any) {
  6. const d = new Date(date)
  7. const now = Date.now()
  8. const diff = (now - d.getTime()) / 1000
  9. if (diff < 30) {
  10. return '刚刚'
  11. } else if (diff < 3600) {
  12. // less 1 hour
  13. return Math.ceil(diff / 60) + '分钟前'
  14. } else if (diff < 3600 * 24) {
  15. return Math.ceil(diff / 3600) + '小时前'
  16. } else if (diff < 3600 * 24 * 2) {
  17. return '1天前'
  18. }
  19. return Math.ceil(diff / (3600 * 24)) + '天前'
  20. }
  21. const data = ref((await service.get(`/post/getNewPost?num=5`)).data)
  22. data.value.map((item: any) => {
  23. item.createdAt = formatDate(item.createdAt)
  24. })
  25. const tabclick = (row: any) => {
  26. window.location.href = `/post/${row.id}`
  27. }
  28. </script>
  29. <template>
  30. <el-table :data="data"
  31. style="width: 100%"
  32. @cell-click="tabclick">
  33. <el-table-column prop="title"
  34. label="标题" />
  35. <el-table-column prop="createdAt"
  36. label="发表时间" />
  37. </el-table>
  38. </template>
  39. <style scoped >
  40. </style>