As a general practice, create the directives folder in the SRC folder and create the index.js file to write the custom directives and export them as needed, e.g

Export const imgerror = {// Inserted to current component, Inserted (el,binding){// the element where the el directive is inserted // img.onerror El.onerror = () => {// Binding directive information object => binding.name(directive name) binding.value(directive value) el.src = binding.value}}Copy the code

Import in main.js

Import * as cache from '@/ cache '// Directive (key, cache [key])} for in is unordered (the object is a collection of directives) for(const key in directives) {vue. directive(key, cache [key])} Keys (obj) can collect all keys in an object and store them in an array (array can be sorted, filtered, etc.). Object.keys(rectives).forEach(item => { Vue.directive(item, directives[key]) }Copy the code