从 Vue.js 中的方法调用全局过滤器

问题描述

我从这里使用了一个全局过滤器:https://www.npmjs.com/package/@vuejs-community/vue-filter-date-format 来格式化我可以从 html 调用的日期,如下所示:

{{new Date(VALUE) | dateFormat('MMM. DD,YYYY',dateFormatConfig)}}

我试图从一个方法调用它。这就是 Vuejs 所说的调用方式。

this.$options.filters.FILTERNAME(VALUE)

但我不知道如何进行这项工作。这不起作用,因为没有 VALUE

this.$options.filters.dateFormat('MMM. DD,dateFormatConfig)

我也试过这个:

this.$options.filters.dateFormat('MMM. DD,VALUE)

还有这个:

this.$options.filters.dateFormat(VALUE,'MMM. DD,YYYY')

还有这个:

this.$options.filters.dateFormat('MMM. DD,dateFormatConfig)(VALUE)

这些都不行。有谁知道如何从方法中正确调用它?

更新:

事实证明这确实有效:

this.$options.filters.dateFormat(VALUE,YYYY')

我需要将 VALUE 转换为:new Date(VALUE)

所以最终结果是:

this.$options.filters.dateFormat(new Date(VALUE),YYYY')

解决方法

我认为您的过滤器注册有问题。在您的 index.js(主应用程序文件)中,您应该导入为 import VueFilterDateFormat from '@vuejs-community/vue-filter-date-format'; 然后Vue.use(VueFilterDateFormat); 或者您可以在某些组件中本地注册它。 然后你就可以像基本用法一样使用它了 [here]