You probably missed importing the desired filter. What I advise to do in case of a global filter:
- Create a separate js file, in a Filters folder for example. Each file will be a filter, example under a simple filter:
name-substr-format.js
export function nameSubstrFormat (name, number = 33) {
if (name) {
return name.substring(0, number)
}
return ''
}
The above filter is a file that exports only one function, in which case its functionality is to limit the amount of characters in a string. If nothing passes, returns 33 characters at most.
- Now import it into your
main.js
and call him in his instance Vue:
import { nameSubstrFormat } from './filters/name-substr-format'
Vue.filter('nameSubstrFormat', nameSubstrFormat)
- To use it directly in your HTML, just call it within an interpolation, after the pipe ("|"), the first parameter becomes the interpolation variable.
<span>{{ nome | nameSubstrFormat(20) }}</span>
Note that it can be used both with variables within your data()
, how many computed variables (computed properties).