vue2?對(duì)全局自定義指令一次性進(jìn)行注冊(cè)的方法
在src下新建文件夾directive/index.js
index.js文件中用來(lái)寫入注冊(cè)方法
/*
* @Author: 周云芳
* @Date: 2022-06-28 15:21:41
* @LastEditors: 周云芳
* @LastEditTime: 2022-06-28 16:44:16
* @Description: 用于自動(dòng)注冊(cè)全局自定義指令
* 使用規(guī)則:
* 根據(jù)導(dǎo)出的name在頁(yè)面使用如directives對(duì)象中的name為el-drag-dialog,頁(yè)面使用:v-el-drag-dialog
*/
const requireDirective = require.context('./', true, /\.js$/)
// 自定義指令
let directives = {}
requireDirective.keys().map((file) => {
// console.log('file', file)
const name = removerLastIndex(file)
if (name) {
directives = {
...directives,
[name]: requireDirective(file).default
}
}
return false
})
function removerLastIndex (str) {
const start = str.substring(2, str.length) // 去除路徑中的./ start=el-drag-dialog/index.js
// str = login/index
// 獲取最后一個(gè)/的索引
const index = start.lastIndexOf('/')
// 獲取最后一個(gè)/后面的值
const val = start.substring(index + 1, start.length)
// 判斷是不是index結(jié)尾
if (val === 'index.js') {
return start.substring(index, -1)
}
return str
}
console.log('directives', directives)
export default {
install (Vue) {
Object.keys(directives).forEach((key) => {
console.log('directives[key]', key, directives[key])
Vue.directive(key, directives[key])
})
}
}示例自定義指令頁(yè)面:
src\directive\el-drag-dialog\index.js
/*
* @Author: 周云芳
* @Date: 2022-06-28 15:11:03
* @LastEditors: 周云芳
* @LastEditTime: 2022-06-28 16:31:15
* @Description: 自定義指令示例
*/
export default {
inserted: function (el, binding) {
el.addEventListener('click', () => {
console.log('指令點(diǎn)點(diǎn)')
})
}
}main.js引入
import Directive from '@/directive' Vue.use(Directive)
頁(yè)面用使用:
<!--
* @Author: 周云芳
* @Date: 2022-06-28 09:18:44
* @LastEditors: 周云芳
* @LastEditTime: 2022-06-28 16:29:28
* @Description: 文件描述
-->
<template>
<div>
測(cè)試
<auto-test v-el-drag-dialog></auto-test>
</div>
</template>
<script>
export default {
name: 'test',
props: [''],
data () {
return {}
},
}
</script>
<style lang="" scoped></style>效果:

到此這篇關(guān)于vue2 對(duì)全局自定義指令一次性進(jìn)行注冊(cè)的文章就介紹到這了,更多相關(guān)vue全局自定義指令注冊(cè)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用VueCli3+TypeScript+Vuex一步步構(gòu)建todoList的方法
這篇文章主要介紹了使用VueCli3+TypeScript+Vuex一步步構(gòu)建todoList的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07
基于el-tree實(shí)現(xiàn)懶加載穿梭條的示例代碼
這篇文章主要介紹了基于el-tree實(shí)現(xiàn)懶加載穿梭條的示例代碼,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2024-03-03
ant-design-vue中的table自定義格式渲染解析
這篇文章主要介紹了ant-design-vue中的table自定義格式渲染,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10
Vue3實(shí)現(xiàn)雙向綁定的基本原理和代碼示例解析
Vue3中的響應(yīng)式系統(tǒng)由Proxy實(shí)現(xiàn),它取代了Vue2中的Object.defineProperty,提供了更強(qiáng)大的功能和更好的性能,下面給大家介紹Vue3實(shí)現(xiàn)雙向綁定的基本原理和代碼示例解析,感興趣的朋友一起看看吧2024-12-12
解決vue中props對(duì)象中設(shè)置多個(gè)默認(rèn)值的問(wèn)題
props中設(shè)置了默認(rèn)值,但是獲取時(shí)(獲取父頁(yè)面沒有傳的屬性) 打印出來(lái)是undefined,所以本文給大家介紹了解決vue中props對(duì)象中設(shè)置多個(gè)默認(rèn)值的問(wèn)題,需要的朋友可以參考下2024-04-04
vue3中輕松實(shí)現(xiàn)switch功能組件的全過(guò)程
這篇文章主要給大家介紹了關(guān)于vue3中輕松實(shí)現(xiàn)switch功能組件的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
vue.js基于v-for實(shí)現(xiàn)批量渲染 Json數(shù)組對(duì)象列表數(shù)據(jù)示例
這篇文章主要介紹了vue.js基于v-for實(shí)現(xiàn)批量渲染 Json數(shù)組對(duì)象列表數(shù)據(jù),結(jié)合實(shí)例形式分析了vue.js使用v-for遍歷json格式數(shù)據(jù)渲染列表相關(guān)操作技巧,需要的朋友可以參考下2019-08-08

