简体   繁体   English

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

[英]Call a global filter from a method in Vue.js

I have a global filter I am using from here: https://www.npmjs.com/package/@vuejs-community/vue-filter-date-format to format a date that I can call from the html like so:我有一个从这里使用的全局过滤器: https://www.npmjs.com/package/@vuejs-community/vue-filter-date-format来格式化我可以从 html 调用的日期,如下所示:

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

I am trying to call this from a method.我试图从一种方法中调用它。 This is how Vuejs says to call it.这就是 Vuejs 所说的调用它的方式。

this.$options.filters.FILTERNAME(VALUE)

But I do not know how to make this work.但我不知道如何使这项工作。 This does not work because there is no VALUE这不起作用,因为没有 VALUE

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

I have also tried this:我也试过这个:

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

and this:和这个:

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

and this:和这个:

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

None of these work.这些都不起作用。 Does anyone know how to call this properly from a method?有谁知道如何从方法中正确调用它?

UPDATE:更新:

Turns out this does work:事实证明这确实有效:

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

I needed to convert the VALUE to this: new Date(VALUE)我需要将 VALUE 转换为: new Date(VALUE)

So the end result is:所以最终的结果是:

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

I think there is a problem with your filter registration.In your index.js(main app file), you should import as import VueFilterDateFormat from '@vuejs-community/vue-filter-date-format';我认为您的过滤器注册存在问题。在您的 index.js(主应用程序文件)中,您应该导入为import VueFilterDateFormat from '@vuejs-community/vue-filter-date-format'; and then Vue.use(VueFilterDateFormat);然后Vue.use(VueFilterDateFormat); or you can register it locally in some components.或者您可以在某些组件中本地注册它。 Then you will be able to use it as the basic usage indicates [ here ]然后您将能够使用它,因为基本用法表明[ 这里]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM