简体   繁体   English

我如何使用应与 Vue 3 兼容的 Vue 2 使用过滤器

[英]How can i use filter using Vue 2 which should be compatible for Vue 3

I am using Vue js 2 + version and for my requirement i created a filter for date formatting but i heard that in Vue 3 filter got removed and instead need to use compute or method the problem here how can i write a compatible version for version 2 and version 3我正在使用 Vue js 2 + 版本,并且根据我的要求,我创建了一个用于日期格式的过滤器,但我听说在 Vue 3 中过滤器被删除了,而是需要使用计算或方法,这里的问题如何为版本 2 编写兼容版本和版本 3

Vue js 2 filter i wrote我写的 Vue js 2 过滤器

<ul>
       
  <li v-for="(info, index) in data">
     {{info.time.data | Filter}}
  <li>
<ul>

Filter request :过滤请求:

    filters: {
  Filter(date){
     return moment.unix(date).utc().format('HH:mm a');
    
    }
  },

How can i write a compatible method which can be used for both version 2 & 3我如何编写可用于版本 2 和 3 的兼容方法

As stated here https://v3.vuejs.org/guide/migration/filters.html#overview - filters using pipe operators will not work in Vue 3, and have to be replaced with methods or computed properties.如此处所述https://v3.vuejs.org/guide/migration/filters.html#overview - 使用管道运算符的过滤器在 Vue 3 中不起作用,必须替换为方法或计算属性。

It would work both on Vue 2 and Vue 3 if You put timeFilter into methods and change Your template replacing pipe with method call .如果您将timeFilter放入methods并更改您的模板,将管道替换为方法调用,则它在Vue 2Vue 3 上都可以使用。

But probably computed property would be better optimized since it should cache values (resulting in less operations if input value hasn't changed).但可能计算属性会得到更好的优化,因为它应该缓存值(如果输入值没有改变,则会导致更少的操作)。

<ul>
  <li v-for="(info, index) in data" :key="index">
     {{ timeFilter(info.time.data) }}
  <li>
<ul>
methods: {
  timeFilter(date) {
     return moment.unix(date).utc().format('HH:mm a');
  }
}

We can also use global $filters property as suggested in migration strategy (and also @Naren suggested).我们还可以按照迁移策略中的建议(以及@Naren 建议)使用全局 $filters属性。

In Vue 2 and Vue 3 templates:在 Vue 2 和 Vue 3 模板中:

<ul>
  <li v-for="(info, index) in data" :key="index">
     {{ $filters.timeFilter(info.time.data) }}
  <li>
<ul>

In Vue 3:在 Vue 3 中:

// main.js
const app = createApp(App);

app.config.globalProperties.$filters = {
   timeFilter: date => moment.unix(date).utc().format('HH:mm a')
}

In Vue 2:在 Vue 2 中:

Vue.prototype.$filters = {
   timeFilter: date => moment.unix(date).utc().format('HH:mm a')
}

Vue 3 does not support filters, As per Vue docs: Filters are removed from Vue 3.0 and no longer supported. Vue 3 不支持过滤器,根据 Vue 文档: Filters are removed from Vue 3.0 and no longer supported. But you can still use computed properties for data change listeners otherwise methods suffice.但是您仍然可以将computed properties用于数据更改侦听器,否则methods就足够了。 In case if you want to register them globally, use can do like the following如果你想全局注册它们,使用可以像下面这样

// main.js
const app = createApp(App)

app.config.globalProperties.$filters = {
   timeFilter: (date) => moment.unix(date).utc().format('HH:mm a')
}
<ul>
  <li v-for="(info, index) in data">
     {{ $filters.timeFilter(info.time.data) }}
  <li>
<ul>

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

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