初始化
This commit is contained in:
9
src/directives/index.js
Normal file
9
src/directives/index.js
Normal file
@@ -0,0 +1,9 @@
|
||||
export default {
|
||||
install(app) {
|
||||
const directivesList = import.meta.glob('./*.js', { eager: true });
|
||||
// 遍历指令文件实现自动注册
|
||||
Object.keys(directivesList).forEach(key => {
|
||||
app.directive(directivesList[key].default.name, directivesList[key].default);
|
||||
});
|
||||
}
|
||||
};
|
||||
16
src/directives/loadimg.js
Normal file
16
src/directives/loadimg.js
Normal file
@@ -0,0 +1,16 @@
|
||||
// 加载图片
|
||||
export default {
|
||||
name: 'loadimg',
|
||||
mounted(el, binding) {
|
||||
const src = binding.value
|
||||
if (el.src === src) return
|
||||
const img = new Image()
|
||||
img.src = src
|
||||
img.onload = () => {
|
||||
el.src = src
|
||||
}
|
||||
img.onerror = () => {
|
||||
console.log('图片加载失败:', src)
|
||||
}
|
||||
},
|
||||
};
|
||||
Reference in New Issue
Block a user