当前位置: 首页 > 知识库问答 >
问题:

如何在Vue 3项目中显示用date-fns格式化的Fi还原时间戳字段?

乐城
2023-03-14

我试图在DOM中使用date-fns date实用程序库的格式显示createdAt字段,该格式返回给定日期和now之间的距离,例如“不到一分钟”前。

例如,在给定的Firestore文档中,createdAttimestamp类型,值为11/14/2021 10:49:09 AM

我能够访问和显示createdAt字段,如下所示:

我试图显示date-fns格式化日期如下:

const computedDateToNow = computed(() => {
  return formatDistanceToNow(review.createdAt.toDate())
})

console.log(computedDateToNow)

我在控制台得到的错误是

Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'toDate')
    at ReactiveEffect.eval [as fn] (ReviewDetails.vue?5986:590)
    at ReactiveEffect.run (reactivity.esm-bundler.js?a1e9:160)
    at ComputedRefImpl.get value [as value] (reactivity.esm-bundler.js?a1e9:1087)
    at unref (reactivity.esm-bundler.js?a1e9:1001)
    at Object.get (reactivity.esm-bundler.js?a1e9:1004)
    at Proxy.render (ReviewDetails.vue?5986:34)
    at renderComponentRoot (runtime-core.esm-bundler.js?5c40:756)
    at ReactiveEffect.componentUpdateFn [as fn] (runtime-core.esm-bundler.js?5c40:4594)
    at ReactiveEffect.run (reactivity.esm-bundler.js?a1e9:160)
    at callWithErrorHandling (runtime-core.esm-bundler.js?5c40:6987)

review.createdAtreview.createdAt.toDate()在DOM中的

为什么toDate()方法(链接到Firebase文档中的方法)在computedDateToNow中引起问题?

UPDATE基于这个注释,这是很可能的,这个javascript函数被放置在实际的html被加载之前,我添加了一个if(review.createdat)语句,错误消失了,但是review.createdatconsole.log中仍然未定义(computedDateToNow)

下面是带有if语句的代码块:

const computedDateToNow = computed(() => {
    if (review.createdAt) {
      console.log('begin new console dot log',review.createdAt,'end new console dot log')
      return formatDistanceToNow(review.createdAt.toDate())
    }
      
    })

添加(响应@Raffobaffo的请求):

<script>

import useDocument from '@/composables/useDocument'
import getDocument from '@/composables/getDocument'
import { computed } from 'vue'
import { formatDistanceToNow } from 'date-fns'

export default {
  props: ['id'],
  components: { },
  setup(props) {
    const { error, document: review } = getDocument('reviews', props.id)

    const { deleteDoc, updateDoc } = useDocument('reviews', props.id)


// BEGIN formatting timestamp

console.log('begin new console dot log',review.createdAt,'end new console dot log')

    const computedDateToNow = computed(() => {
    if (review.createdAt) {
      console.log('begin new console dot log',review.createdAt,'end new console dot log')
      return formatDistanceToNow(review.createdAt.toDate())
    }
      
    })

    console.log(computedDateToNow)
    
// END formatting timestamp



    return { error, review, formatDistanceToNow, computedDateToNow }  
  }
}
</script>

感谢任何帮助!


共有2个答案

张逸清
2023-03-14

一个点位于计算属性上:仅当检查通过时才返回一个值,而计算属性应始终返回某个值。

因此,我宁愿尝试这种方法:

const computedDateToNow = computed(() => {
if (review.createdAt) {
  console.log('begin new console dot log',review.createdAt,'end new console dot log')
  return formatDistanceToNow(review.createdAt.toDate())
}
  return 'not-available';
})
阎劲
2023-03-14

我建议使用npm软件包管理器:

在这种情况下,您可以直接在组件中使用过滤器。

// my-component.js
import { dateFilter } from "vue-date-fns";
 
export default {
    filters: {
        date: dateFilter
    }
}

<!-- my-component.vue -->
<template>
    <div>Now: {{ new Date() | date }}</div>
</template>

如果您想继续使用当前代码,也许可以尝试调试const computedatenow,以查看您得到的值。

根据你分享的date-fns的留档,这里有一个console.log的例子:

console.log(format(new Date(), "'Today is a' eeee"))

输出应为:

//=> "Today is a Wednesday"

这将帮助您验证函数是否执行良好;如果没有,验证库的导入。

一个建议是将数据类型从const更改为var。请记住,var可以更新和重新声明,const不仅不能重新声明,而且不能重新分配。

 类似资料:
  • 问题内容: 如何根据自己的喜好设置java.sql时间戳?(以字符串形式显示) 问题答案: 延伸。你可以做: 或还包括时间:

  • 我在Firebase上运行一个web应用程序,从实时数据库开始,现在我正在将数据迁移到firestore。 我使用ADMINSDK迁移了除时间戳之外的所有其他数据。在我的web应用程序中,当用户上传新内容时,代码会向数据库添加时间戳,以便内容可以按时间排序。 问题在于实时数据库和firestore显然在记录时间戳时使用了不同的方法: 另一个问题是firebase.firestore.Timesta

  • 问题内容: 有没有一种方法可以将UTC时间格式化为我想要的Java中任意字符串格式?基本上,我在考虑让某个类使用时间戳,然后传递它是字符串,告诉它如何格式化,然后它为我返回格式化的字符串。有没有办法做到这一点? 问题答案: java.text.SimpleDateFormat类以对语言环境敏感的方式提供日期的格式设置和解析。 SimpleDateFormat的javadoc标头是详细信息的很好来源

  • 本文向大家介绍php时间戳格式化显示友好的时间函数分享,包括了php时间戳格式化显示友好的时间函数分享的使用技巧和注意事项,需要的朋友参考一下 在项目中时间一律显示为2014-10-20 10:22显得很呆板。在微博、QQ空间等网站通常会显示为几秒前,几分钟前,几小时前等容易阅读的时间,我们称之为友好的时间格式。那么用php怎么实现呢? 大体思路如下: 如果是跨年并且大于3天就显示为具体的时间 如

  • 本文向大家介绍php格式化时间戳,包括了php格式化时间戳的使用技巧和注意事项,需要的朋友参考一下 CMS中一般显示时间比较新的文章需要显示几分钟前,几天前这样,但是一般数据库里面记录的都是时间戳(至少我习惯这样),所以就需要一个转化的过程,根据网上的资料加上自己的修改整理封装了两段代码 下面是封装好的方法 或者 更详细的 再分享一个类似的