1. Create a new filter.js file and put all global filters in it
// filter.js const filter = { sliceTxt: (txt, length) => { if (length > 120) { let newTxt = txt.slice(0, 120) return newTxt } else { return txt } } }
2. Global registration in main.js
// main.js import filter from '../filter' for (let key in filter){ //must be placed before new Vue View.filter(key, filter[key]) }
3. Use in vue files
<P>{{item | slice(140)}}</p> // item is the default parameter of the filter, which corresponds to txt here, and 140 is the second parameter
If you don't want to create a new filter.js file, you can register it directly in mian.js:
// main.js Vue.filter('filterName', txt => { return txt })
Official document address: click to open the link