-
-
Save AleksejDix/792b29256eef845d618731a296036690 to your computer and use it in GitHub Desktop.
Map global filters to local methods to use them in components as well as templates
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Map global filters for being used from within `methods`. This makes them usable in js as well as templates | |
* @param {Array} filters - A list of registered filter names | |
* @return {Object) An object containing filters and their functions | |
*/ | |
export function mapFilters(filters) { | |
return filters.reduce((result, filter) => { | |
result[filter] = function(...args) { | |
return this.$options.filters[filter](...args); | |
}; | |
return result; | |
}, {}); | |
} | |
// components.vue | |
export default { | |
methods: { | |
...mapFilters(['baseURL', 'removeParams']), | |
}, | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment