This time I will show you how to use filter globally, what are the precautions for global use of filter, the following is a practical case, let's take a look.
Officially given
Vue.filters(id , [definition]) //id {string} //definition {function}
View details
If we have multiple filters in the project, how can I register them at once and make them available globally? We create a new filter in the project Folder, as follows, index.js is the export file, readMore is a filter
that processes
File directory
Paste the code below:
//index.js // 引入所有的过滤函数 import readMore from './readMore'; // 导出在一个对象上 export default { readMore }; //readMore.js //查看更多文字显示'...' let readMore = (text,length,suffix) => { if(text) { if(text.length <p style="text-align: left;">Then do the following processing in main.js: </p><p style="text-align: left;"><img src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/061/021/291b9a26604affe749aca9aa2b99c650-1.png?x-oss-process=image/resize,p_40" class="lazy" alt=""></p><p style="max-width:90%"><strong>main.js does global registration</strong></p><pre class="brush:php;toolbar:false">//全局注册自定义的过滤器 import filters from './filters'; for(let key in filters){ Vue.filter(key, (val,value1,value2) => { return filters[key](val,value1,value2); }); }
and you can use it globally
//在test.vue里面使用 <p>全文`)"></p> <span>#<span>{{'文字文字' | readMore(15,'...')}}</span>#</span>
I believe you have mastered the method after reading the case in this article. Please pay attention to php for more exciting things. Other related articles on the Chinese website!
Recommended reading:
What to do if the actual project is not in the root directory after compilation
How to get the value of the class in the dom
The above is the detailed content of How to use filter globally. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Linux new version
SublimeText3 Linux latest version

SublimeText3 Chinese version
Chinese version, very easy to use

Atom editor mac version download
The most popular open source editor

SublimeText3 Mac version
God-level code editing software (SublimeText3)