Home >Web Front-end >JS Tutorial >Detailed explanation of vue filter filter instance

Detailed explanation of vue filter filter instance

亚连
亚连Original
2018-05-30 15:22:001957browse

VueJs provides a powerful filter API that can perform various filtering processes on data and return the required results. This article mainly introduces Vue filter examples to everyone. Interested friends should learn together.

Vue filters are generally at the end of JavaScript expressions, indicated by the "|" symbol:

Filters can make our code more beautiful and can generally be used for time formatting, capitalizing first letters, etc.

For example: {{ date | dateFormat }}This is how to write a filter; {{ dateFormat(date) }}This is how to write a function call

It can be seen that the filter is written in a more semantic way, allowing people to see its meaning at a glance.

<!-- 在双花括号中 -->
{{ message | capitalize }}
<!-- 在 `v-bind` 中 -->
<p v-bind:id="rawId | formatId"></p>
<!-- 也可以串联多个过滤器 -->
{{ message | filterA | filterB }}

// In this example, filterA is defined as a filter function that receives a single parameter, and the value of the expression message will be passed into the function as a parameter middle. 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 | capitalize(&#39;string&#39;, obj) }}


// The parameters here will start from the second parameter in the filter function. The first parameter is the value message to be filtered, that is, 'string' is the second parameter and obj is the third parameter.

After the filter method receives the parameters, you can perform a series of processes within the method and finally return the processing results.

1. The filter can be

filters: {
 capitalize: function (value) {
 if (!value) return &#39;&#39;
 value = value.toString()
 return value.charAt(0).toUpperCase() + value.slice(1)
 }
}

2. The filter can also be Mounted in global Vue

Vue.filter(&#39;capitalize&#39;, function (value) {
 if (!value) return &#39;&#39;
 value = value.toString()
 return value.charAt(0).toUpperCase() + value.slice(1)
})

The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.

Related articles:

detailed explanation of echarts mouse overlay highlighting node and relationship names

vue realizes copying content to paste Board clipboard method

vue method to get the currently activated route

The above is the detailed content of Detailed explanation of vue filter filter instance. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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