Understanding filters: a tool for formatting data

  1. Filters are used for text formatting, only as formatting operations, not complex business processing
  2. The use of filter method: use through the pipe symbol (|)
  3. Places to use filters: Mustache interpolation and V-bing expressions

Two, the use of filters

// How to use the filter?We use a named pipe to use filters, for example: data | filter namefillters:{filter name (value){// Parameters are filtered data
        
        returnData;// What does return show}}Copy the code

Three, filter declaration form:

// Filters also have two declarations

    // Global declaration (written in main.js)Vue. Filter (filter name, callback function) The callback function has a value value that represents the value of the formatted data// Local declaration
      new Vue({
        filters: {
          'Filter name':function( value ){
            return}}})Copy the code

Example: The filter is used with the moment.js plugin

// Local declaration is used
/ / template
<template>{{time |fullTime}}</template>

/ / import moment. Js
import moment from 'moment'
export default {
  name:'goods'.data() {
    return {
    time:1514865808,},filters: {fullTime(val){
      return moment(val).format("YYYY-MM-DD hh:mm:ss "); }},//fullTime final display result :1970-01-18 08:47:45
Copy the code
The above is purely personal understanding, I hope to help you, a vegetable chicken program yuan ~~