Vue global filter concepts, precautions and basic usage methods

Vue global filter concepts, precautions and basic usage methods

1. The concept of filter

Vue.js allows you to customize filters, which can be used for some common text formatting. Filters can be used in two places: mustache interpolation and v-bind expressions

1. Customize the format of a global filter

Vue.filter('the name of the filter when it is called in the future', the filter processing function)

2. How to call the filter

 <!-- When calling a filter, you need to use | to call it, | is called a pipe character -->
      <td>{{item.ctime | formatDate}}</td>
In the filter processing function, the first parameter is fixed and is always the value before the pipe character.
// The data here is the value of item.ctime before the pipe character Vue.filter('formatDate',function(data){

})
// The filter must have a return value

3. Notes on filters

  • Vue.filter('filter name', filter processing function)
  • Note: In the filter processing function, the first parameter has a defined function and is always the value before the pipe character.
  • Call the filter {{item.ctime | formmatDate}} When calling the filter, you need to use | to call it, which is called the pipe character.
  • You can pass parameters when calling the filter, {{item.ctime | formmatDate('傳遞參數')}}
  • Note: The parameters passed by calling the filter can only be received from the second parameter of the processing function, because the first parameter has been occupied by the value before the pipe character.
  • Note: The filter's processing function must return a value
  • You can use the pipe character to call multiple filters continuously. The final output result is always based on the last filter.
  • Note: Filters can only be used in interpolation expressions or v-bind , not in other places, such as v-text

4. Basic usage

Render a sentence on the page through the vue interpolation expression
  <div id="app">
        <h3>{{mes}}</h3>
    </div>
<script src="./js/vue.js"></script>
    <script>
        let vm = new Vue({
            el:'#app',
            data:{
                mes: "I am a pessimistic person, and pessimistic people do pessimistic things"
            }
        })
    </script>
Requirements: Requirements: Replace the word "pessimistic" with "cheerful", provided that the mes source data in Vue cannot be changed

First, customize a global filter in the script tag and name the filter yourself:
 Vue.filter('setStr',function(data){
        })
Define a method in the filter:
 Vue.filter('setStr',function(data){
            // The filter must have a return value return data.replace(/pessimistic/g,'cheerful')
            // Use the string operation method replace to replace certain elements in the string with other elements, g represents global match})

Then call the filter in the interpolation expression
<div id="app">
        <h3>{{mes | setStr}}</h3>
    </div>

Now go to the page to view the effect:
A basic filter is defined
We can also give the formal parameter in the filter function, without giving the character to be replaced in the method.
Vue.filter("strFormat",function(data,str){ // You can give a parameter after data // In the filter, there must be a return value return data.replace(/pessimistic/g,str)
            // Use the string operation method replace to replace certain elements in the string with other elements, g represents global match})
Then give the actual parameters when calling:
<div id="app">
        <h3>{{mes | setStr("careless")}}</h3>
    </div>
View the results:
You can also give default values ​​in the formal parameters. If no actual parameters are given when calling, the default values ​​are output. If actual parameters are given, the values ​​of the actual parameters are output.
<div id="app">
        <h3>{{mes | setStr}}</h3>
    </div>

    <script src="./js/vue.js"></script>
    <script>

        Vue.filter('setStr',function(data,str="Careful"){
            // The filter must have a return value return data.replace(/pessimistic/g,str)
            // Use the string operation method replace to replace certain elements in the string with other elements, g represents global match})

The result is:

This concludes this article about the concepts, precautions, and basic usage of vue global filters. For more relevant vue global filter content, please search for previous articles on 123WORDPRESS.COM 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?
  • Detailed explanation of Vue filters
  • What you need to know about filters in Vue
  • 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

<<:  CSS achieves the effect of hiding the scroll bar and scrolling the content (three ways)

>>:  Mysql database recovery actual record by time point

Recommend

A brief analysis of kubernetes controllers and labels

Table of contents 01 Common controllers in k8s RC...

Methods for deploying MySQL services in Docker and the pitfalls encountered

I have been learning porters recently. I feel lik...

MySql 5.7.20 installation and configuration of data and my.ini files

1. First download from the official website of My...

Vue implements local storage add, delete and modify functions

This article example shares the specific code of ...

Nginx reverse proxy to go-fastdfs case explanation

background go-fastdfs is a distributed file syste...

Detailed explanation of special phenomena examples of sleep function in MySQL

Preface The sleep system function in MySQL has fe...

In-depth explanation of iterators in ECMAScript

Table of contents Preface Earlier iterations Iter...

Detailed steps to install Docker mongoDB 4.2.1 and collect springboot logs

1: Install mongodb in docker Step 1: Install mong...

MySQL index principle and usage example analysis

This article uses examples to illustrate the prin...

Implementation of WeChat applet message push in Nodejs

Select or create a subscription message template ...

Example of how to change the domestic source in Ubuntu 18.04

Ubuntu's own source is from China, so the dow...

Detailed steps for installing MinIO on Docker

Table of contents 1. Check whether the docker env...

Steps to install MySQL 5.7 in binary mode and optimize the system under Linux

This article mainly introduces the installation/st...