What you need to know about filters in Vue

What you need to know about filters in Vue

Preface

Hello everyone, today I will share some tips on filters in Vue.

What is a filter

Vue.js allows you to define your own filters, which can be used for some common text formatting. Filters can be used in two places: double curly brace interpolation and v-bind expressions (the latter is supported since 2.1.0+).

How to use filters

Global Filters

  1. This case is to filter out ¥ and 元 in the price
  2. Example¥1999.00
  3. Define a capitalize method and pass in the value
  4. If value is empty, returns an empty string
  5. Otherwise, the string "¥" "元" is added, where toFixed(2) is rounded to the specified number of decimal places.

How to use

//main.js
Vue.filter("capitalize", function (value) {
  if (!value) return "";
  return "¥" + value.toFixed(2) + "元";
});

Use within double curly braces

<!-- home.vue -->
      <h1>Vue Filters</h1>
      <p>{{ price | capitalize }}</p>
      {{ 20.6664376486 | capitalize }}

Use in v-bind

      <h1>Vue Filters</h1>
      <p :id="122 | capitalize"></p>

Local filter

Note here that when the global filter and the local filter have the same name, the local filter will be used.

Local filters You can define local filters in a component's options:

export default {
  name: 'index',
  data() {
    return {
      price: 1999
    }
  },
  filters:
    capitalize: function (value) {
      if (!value) return ''
      return '¥' + value.toFixed(2) + '元'
    }
  }
}

Filters can be connected in series

In this example, filterA is defined as a filter function that accepts a single argument. The value of the expression message is passed into the function as an argument. Then continue to call the filter function filterB, which is also defined to receive a single parameter, and pass the result of filterA to filterB.

{{ message | filterA | filterB }}

Notice:

1. When there are two filters with the same name, local and global, they will be called based on the proximity principle, that is, the local filter will be called before the global filter!

2. An expression can use multiple filters. Filters need to be separated by pipe characters "|". The execution order is from left to right

Summarize

This is the end of this article about the must-know knowledge about filters in Vue. For more relevant Vue filter knowledge, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Do you really understand Vue's filters?
  • How much do you know about Vue.js filters?
  • Vue global filter concepts, precautions and basic usage methods
  • Detailed explanation of Vue filters
  • Detailed explanation of filter instance code in Vue
  • Vue filter usage example analysis
  • Use of filters in Vue2.0 series
  • Tutorial on using filters in vue.js
  • Usage of filters in Vue

<<:  DOCTYPE type detailed introduction

>>:  10 performance configuration items that need to be adjusted after installing MySQL

Recommend

How to configure MySQL8 in Nacos

1. Create the MySQL database nacos_config 2. Sele...

MySQL Query Cache and Buffer Pool

1. Caches - Query Cache The following figure is p...

CSS3 to achieve dynamic background gradient effect

Learning CSS3 is more about getting familiar with...

How to solve the problem of character set when logging in to Linux

Character set error always exists locale: Cannot ...

Details on using regular expressions in MySQL

Table of contents 1. Introduction 2. Prepare a pr...

How InnoDB cleverly implements transaction isolation levels

Preface In the previous article Detailed Explanat...

Summary of web designers' experience and skills in learning web design

As the company's influence grows and its prod...

MySQL 8.0.20 installation and configuration tutorial under Win10

MySQL 8.0.20 installation and configuration super...

HTML Basics: The basic structure of HTML

The basic structure of HTML hypertext documents is...

How to install ElasticSearch on Docker in one article

Table of contents Preface 1. Install Docker 2. In...

How to install the graphical interface in Linux

1. Linux installation (root user operation) 1. In...

IE6 space bug fix method

Look at the code: Copy code The code is as follows...

18 sets of exquisite Apple-style free icon materials to share

Apple Mug Icons and Extras HD StorageBox – add on...

Html+css to achieve pure text and buttons with icons

This article summarizes the implementation method...