当前位置:网站首页>批量注册组件
批量注册组件
2022-06-23 15:14:00 【@海海人生】
大致步骤:
1 使用 require 提供的函数 context 加载某一个目录下的所有 .vue 后缀的文件。
2 然后 context 函数会返回一个导入函数 importFn
3 它又一个属性 keys() 获取所有的文件路径
4 通过文件路径数组,通过遍历数组,再使用 importFn 根据路径导入组件对象
5 遍历的同时进行全局注册即可
// 其实就是vue插件,扩展vue功能,全局组件、指令、函数 (vue.30取消过滤器)
// 当你在mian.js导入,使用Vue.use() (vue3.0 app)的时候就会执行install函数
// import XtxSkeleton from './xtx-skeleton.vue'
// import XtxCarousel from './xtx-carousel.vue'
// import XtxMore from './xtx-more.vue'
// import XtxBread from './xtx-bread.vue'
// import XtxBreadItem from './xtx-bread-item.vue'
// 导入library文件夹下的所有组件
// 批量导入需要使用一个函数 require.context(dir,deep,matching)
// 参数:1. 目录 2. 是否加载子目录 3. 加载的正则匹配
const importFn = require.context('./', false, /\.vue$/)
// console.dir(importFn.keys()) 文件名称数组
export default {
install (app) {
// app.component(XtxSkeleton.name, XtxSkeleton)
// app.component(XtxCarousel.name, XtxCarousel)
// app.component(XtxMore.name, XtxMore)
// app.component(XtxBread.name, XtxBread)
// app.component(XtxBreadItem.name, XtxBreadItem)
// 批量注册全局组件
importFn.keys().forEach(key => {
// 导入组件
const component = importFn(key).default
// 注册组件
app.component(component.name, component)
})
// 定义指令
defineDirective(app)
}
}
const defineDirective = (app) => {
// 图片懒加载指令 v-lazyload
app.directive('lazyload', {
// vue2.0 inserted函数,元素渲染后
// vue3.0 mounted函数,元素渲染后
mounted (el, binding) {
// 元素插入后才能获取到dom元素,才能使用 intersectionobserve进行监听进入可视区
// el 是图片元素 binding.value 图片地址
const observe = new IntersectionObserver(([{
isIntersecting }]) => {
if (isIntersecting) {
el.src = binding.value
// 取消观察
observe.unobserve(el)
}
}, {
threshold: 0.01
})
// 进行观察
observe.observe(el)
}
})
}
边栏推荐
- 电荷泵原理讲义,电压是怎么“泵”上去的?
- System design and analysis - Technical Report - a solution for regularly clearing verification code
- MySQL advanced statement 2
- 2022年个人理财利率是多少?个人如何选择理财产品?
- 这五年的6个编程感悟!
- JS traversal array (using the foreach () method)
- Gartner's latest report: development of low code application development platform in China
- Detailed steps for MySQL dual master configuration
- How strong is Jingdong's takeout after entering meituan and starving the hinterland?
- glibc nptl库pthread_mutex_lock和pthread_mutex_unlock浅析
猜你喜欢
![[opencv450] salt and pepper noise demo](/img/d8/a367c26b51d9dbaf53bf4fe2a13917.png)
[opencv450] salt and pepper noise demo

30. 串联所有单词的子串

Shandong: food "hidden money", consumption "sweeping monk"

golang 重要知识:RWMutex 读写锁分析

Gartner最新报告:低代码应用开发平台在国内的发展

30. concatenate substrings of all words
![[MAE]Masked Autoencoders掩膜自编码器](/img/08/5ab2b0d5b81c723919046699bb6f6d.png)
[MAE]Masked Autoencoders掩膜自编码器

Raspberry PI installing the wiring pi

Analysis of graphical level-1 programming problem of Electronic Society: cat and mouse

Volatile~ variables are not visible under multithreading
随机推荐
This year's cultural entertainers have turned their sidelines into their main business
[普通物理] 光的衍射
Raspberry PI installing the wiring pi
Gartner's latest report: development of low code application development platform in China
基金开户是有什么风险?开户安全吗
Personal summary of system design and Analysis Course Project
ABP框架之——数据访问基础架构(下)
golang 重要知识:mutex
JS中的pop()元素
JS garbage collection
FPGA 常用缩写及单词在工程领域内的意义
Pop() element in JS
Important knowledge of golang: mutex
xcbdfbs xcvb
mysql 系列:总体架构概述
js遍历数组(用forEach()方法)
SFOD:无源域适配升级优化,让检测模型更容易适应新数据(附论文下载)
Important knowledge of golang: sync Cond mechanism
labelme的JSON文件转成COCO数据集格式
12 BeautifulSoup类的初始化