[英]Call a global filter from a method in 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, YYYY', dateFormatConfig)
我也試過這個:
this.$options.filters.dateFormat('MMM. DD, YYYY', VALUE)
和這個:
this.$options.filters.dateFormat(VALUE, 'MMM. DD, YYYY')
和這個:
this.$options.filters.dateFormat('MMM. DD, YYYY', dateFormatConfig)(VALUE)
這些都不起作用。 有誰知道如何從方法中正確調用它?
更新:
事實證明這確實有效:
this.$options.filters.dateFormat(VALUE, 'MMM. DD, YYYY')
我需要將 VALUE 轉換為: new Date(VALUE)
所以最終的結果是:
this.$options.filters.dateFormat(new Date(VALUE), 'MMM. DD, YYYY')
我認為您的過濾器注冊存在問題。在您的 index.js(主應用程序文件)中,您應該導入為import VueFilterDateFormat from '@vuejs-community/vue-filter-date-format';
然后Vue.use(VueFilterDateFormat);
或者您可以在某些組件中本地注冊它。 然后您將能夠使用它,因為基本用法表明[ 這里]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.