当前位置:网站首页>批量注册组件
批量注册组件
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)
}
})
}
边栏推荐
- 积分商城要如何做才能获取到利润
- Large area and availability area
- [普通物理] 半波损失 等厚与等倾干涉
- Important knowledge of golang: atomic atomic operation
- Personal summary of system design and Analysis Course Project
- The work and development steps that must be done in the early stage of the development of the source code of the live broadcasting room
- 列表查询排序参数处理
- Stone from another mountain - Intelligent Question and answer technology in wechat search
- Print memory station information
- php 二维数组插入
猜你喜欢

mysql 系列:存储引擎

golang 重要知识:sync.Once 讲解

Introduction to the push function in JS

js中的push函数介绍

Important knowledge of golang: atomic atomic operation

Half wave loss equal thickness and equal inclination interference

JS创建一个数组(字面量)

stylegan2:analyzing and improving the image quality of stylegan

Convert JSON file of labelme to coco dataset format

MySQL series: storage engine
随机推荐
js遍历数组(用forEach()方法)
Variable declaration of go language
力扣每日一题-第25天-495.提莫攻击
32. Compose 优美的触摸动画
Analysis of TCP three-time handshake and four-time handshake
How to open a stock account? Is online account opening safe?
Sorting out and summarizing the handling schemes for the three major exceptions of redis cache
要买的理财产品年限长,划算吗?
变压器只能转换交流电,那直流电怎么转换呢?
The "shoulder" of sales and service in the heavy truck industry, Linyi Guangshun deep ploughing product life cycle service
电荷泵原理讲义,电压是怎么“泵”上去的?
CAS操作在ARM和x86下的不同实现
12 BeautifulSoup类的初始化
139. 單詞拆分
Matlab| sparse auxiliary signal denoising and pattern recognition in time series data
Leetcode 450.删除二叉搜索树中的结点
golang 重要知识:waitgroup 解析
PHP指定字段大于100正序排,小于100随机排
MIPI C-PHY协议你了解吗?手机高速接口之一
Mysql database - log management, backup and recovery