Vue过滤器filter

1.Vue.filter(id,[definition])

  • 参数:
    {string} id
    {Function} [definition]
  • 用法:注册或获取全局过滤器
// 注册
Vue.filter('my-filter',function(value) {
  // 返回处理后的值
})
// getter,返回已注册的过滤器
var myFilter = Vue.filter('my-filter')

2.示例:
(1)全局过滤器

Vue.filter('globalFilter',function(value){
  return value + "!!!"
})

(2)组件过滤器

filters: {
  componentFilter: function(value){
    return value + '!!!'
  }
}
基本用法:

(1) 双括号插值:

{{ 'wang' | globalFilter }}

(2)在v-bind表达式中使用

{{ 'wang' | globalFilter }}
过滤器参数写法:

(1){{message | filterA | filterB}}

{{ '2022' | filterA | filterB}}
filters: {
  filterA: function(value){
    return value + '年'
  },
  filterB: function(value){
    return value + 'hello!'
  }
}


// 2022年hello!

(2){{message | filterA('arg1',arg2)}}

<div>{{ '2022' | filterA('11','22')}}</div>  
...
filters: {
  filterA: function(value,arg1,arg2){
    return value + '-' + arg1 + '-' + arg2
  }
}

// 2022-11-22
posted @   silence小铁匠  阅读(290)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~
点击右上角即可分享
微信分享提示