Home > Web Front-end > Vue.js > Take you step by step to understand how to use filters in Vue? A brief analysis of 2 usages

Take you step by step to understand how to use filters in Vue? A brief analysis of 2 usages

青灯夜游
Release: 2022-03-29 20:08:53
forward
2935 people have browsed it

Vue中怎么使用filters过滤器?本篇文章手把手带大家了解一下Vue中filters过滤器的用法,介绍filters过滤器的两种使用方法,希望对大家有所帮助!

Take you step by step to understand how to use filters in Vue? A brief analysis of 2 usages

Vue.js 允许我们自定义过滤器,可被用于一些常见的文本格式化。过滤器可以用在两个地方:双花括号({undefined{ }})插值和 v-bind 表达式 (后者从 2.1.0+ 开始支持)。过滤器应该被添加在 JavaScript 表达式的尾部,由“管道”符号指示。

<!-- 在双花括号中 -->
<div>{{ message | capitalize }}</div>
 
<!-- 在 `v-bind` 中 -->
<div v-bind:id="rawId | formatId"></div>
Copy after login

一、全局过滤器

定义一个全局过滤器很简单,只需要导出一个方法即可。

使用的时候很简单,只需要在入口文件全局引入此过滤器即可,使用 Vue.filter(key, value) 引入。

比如,Java后端返回的时间戳精确到秒,而JS中的时间戳是用毫秒表示,则可以定义一个转换时间戳的全局过滤器:

//main.js
import Vue from &#39;vue&#39;
Vue.filter(&#39;millisecond&#39;, (value) => {
  if (!value) return &#39;&#39;
  value = value.toString()
  return value.charAt(0).toUpperCase() + value.slice(1)
})
Copy after login

在需要使用的组件使用:

<div>{{1516101106 | millisecond}}</div>
Copy after login

全局过滤器之单一挂载

/**
 * dateTmp:要过滤的值
 * fmtTmp:传入的参数,可接收多个参数
 */
<template>
   <!-- 2021-12-20 19:14:18 -->
  <div>{{ 1639998858000 | dateFormat("yyyy/MM/dd HH:mm:ss") }}</div>
</template>
 
Vue.filter(&#39;dateFormat&#39;, function (dateTmp, fmtTmp) {
  let fmt = fmtTmp
  let date = dateTmp
 
  if (!fmt) {
    fmt = &#39;yyyy.MM.dd&#39;
  }
  if (!(date instanceof Date)) {
    date = new Date(date)
  }
  let o = {
    &#39;M+&#39;: date.getMonth() + 1, // 月份
    &#39;d+&#39;: date.getDate(), // 日
    &#39;h+&#39;: date.getHours() % 12 == 0 ? 12 : date.getHours() % 12, // 小时
    &#39;H+&#39;: date.getHours(), // 小时
    &#39;m+&#39;: date.getMinutes(), // 分
    &#39;s+&#39;: date.getSeconds(), // 秒
    &#39;q+&#39;: Math.floor((date.getMonth() + 3) / 3), // 季度
    &#39;S&#39;: date.getMilliseconds() // 毫秒
  }
  let week = {
    &#39;0&#39;: &#39;日&#39;,
    &#39;1&#39;: &#39;一&#39;,
    &#39;2&#39;: &#39;二&#39;,
    &#39;3&#39;: &#39;三&#39;,
    &#39;4&#39;: &#39;四&#39;,
    &#39;5&#39;: &#39;五&#39;,
    &#39;6&#39;: &#39;六&#39;
  }
 
  if (/(y+)/.test(fmt)) {
    fmt = fmt.replace(RegExp.$1, (date.getFullYear() + &#39;&#39;).substr(4 - RegExp.$1.length))
  }
  if (/(E+)/.test(fmt)) {
    fmt = fmt.replace(RegExp.$1, ((RegExp.$1.length > 1) ? (RegExp.$1.length > 2 ? &#39;星期&#39; : &#39;周&#39;) : &#39;&#39;) + week[date.getDay() + &#39;&#39;])
  }
  for (var k in o) {
    if (new RegExp(&#39;(&#39; + k + &#39;)&#39;).test(fmt)) {
      fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : ((&#39;00&#39; + o[k]).substr((&#39;&#39; + o[k]).length)))
    }
  }
  return fmt
});
Copy after login

全局过滤器之批量挂载

//定义方法
//filters.js
export function slice (temp,num) {
    return temp.slice(0,num);
}
Copy after login
//挂载
//main.js
import * as filters from &#39;@/assets/js/filters&#39;;
 
Object.keys(filters).forEach(key => {
    Vue.filter(key, filters[key]);
});
Copy after login
//调用
<!-- 1234 -->
<div>{{ &#39;123456&#39; | slice(4) }} </div>
Copy after login

二、组件过滤器

组件过滤器更简单,只需在对应组件中定义 filters 即可,不过只针对本组件有效。

比如定义一个首字母大写的过滤器:

//定义方法
export default {
  filters: {
    capitalize: function (value) {
      if (!value) return &#39;&#39;
      value = value.toString()
      return value.charAt(0).toUpperCase() + value.slice(1)
    }
  }
}
Copy after login

例子例子

<template>
  <!-- 我要被过... -->
  <div>{{ msg | setSize }}</div>
</template>
 
<script>
export default {
  data() {
    return {
      msg: "我要被过滤",
    };
  },
  filters: {
    setSize(value) {
      if (value.length > 4) {
        return value.splice(0, 4) + "...";
      } else {
        return value;
      }
    },
  },
};
</script>
Copy after login

(学习视频分享:vuejs教程web前端

The above is the detailed content of Take you step by step to understand how to use filters in Vue? A brief analysis of 2 usages. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
vue
source:juejin.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template