install(Vue,option)

  • Vue.js提供install方法,可用于开发新插件以及全局注册组件等
export default {//vue的构造器,option可选的选项对象install(Vue,option){ 组件指令混入挂载vue原型}
}
  • 全局注册组件
// components.js文件中
import ScreenFull from './ScreenFull'
import TagsView from './TagsView'
export default {install(Vue) {Vue.component('ScreenFull', ScreenFull)Vue.component('TagsView', TagsView)}
}
// main.js文件中
import Component from '@/components'
Vue.use(Component)

Vue.directive("指令名",ƒunction)

Vue.directive("指令名",function(el,binding,vnode,oldVnode){})
/* 参数
* @el 指令绑定的元素,可以直接操作DOM
* @binding 一个对象,包含属性
* @vnode vue编译的生成虚拟节点
* @oldVnode 上一次的虚拟节点,仅在update和componentUpdated钩子函数中可用。
* */
Vue.directive('pin', function(el,binding,vnode,oldvnode) {el.style.background = binding.value //背景颜色
})
/* 钩子函数
* @bind 只调用一次,指令第一次绑定到元素时调用
* @inserted 被绑定元素插入父节点时调用
* @update 被绑定元素所在的模板更新时调用,而不论绑定值是否变化
* @componentUpdated 被绑定元素所在模板完成一次更新周期时调用
* @unbind 只调用一次, 指令与元素解绑时调用
* */
Vue.directive("focus", {// 当被绑定的元素插入到 DOM 中时……inserted: function (el) {el.focus();    // 聚焦元素el.value = '初始值'},
});
  • 权限字符指令
// 创建directive文件,包含index.js、hasPermi.js
//index.js中
import hasPermi from './hasPermi'
const install = function(Vue) {Vue.directive('hasRole', hasRole)Vue.directive('hasPermi', hasPermi)
}
export default install// hasPermi.js中
import store from '@/store'
export default {inserted(el, binding, vnode) {const { value } = bindingconst all_permission = "*:*:*";//const permissions = store.getters && store.getters.permissions // 登录后获取的当前登录用户的权限字符数组const permissions = ["but"]  if (value && value instanceof Array && value.length > 0) {const permissionFlag = valueconst hasPermissions = permissions.some(permission => {return all_permission === permission || permissionFlag.includes(permission)})if (!hasPermissions) {el.parentNode && el.parentNode.removeChild(el)}} else {throw new Error(`请设置操作权限标签值`)}}
}// main.js
import directive from './directive'
Vue.use(directive)// 使用
v-hasPermi="['but']"
  • 弹窗拖拽指令
/*** v-dialogDrag 弹窗拖拽* Copyright (c) 2019 ruoyi*/export default {bind(el, binding, vnode, oldVnode) {const value = binding.valueif (value == false) return// 获取拖拽内容头部const dialogHeaderEl = el.querySelector('.el-dialog__header');const dragDom = el.querySelector('.el-dialog');dialogHeaderEl.style.cursor = 'move';// 获取原有属性 ie dom元素.currentStyle 火狐谷歌 window.getComputedStyle(dom元素, null);const sty = dragDom.currentStyle || window.getComputedStyle(dragDom, null);dragDom.style.position = 'absolute';dragDom.style.marginTop = 0;let width = dragDom.style.width;if (width.includes('%')) {width = +document.body.clientWidth * (+width.replace(/\%/g, '') / 100);} else {width = +width.replace(/\px/g, '');}dragDom.style.left = `${(document.body.clientWidth - width) / 2}px`;// 鼠标按下事件dialogHeaderEl.onmousedown = (e) => {// 鼠标按下,计算当前元素距离可视区的距离 (鼠标点击位置距离可视窗口的距离)const disX = e.clientX - dialogHeaderEl.offsetLeft;const disY = e.clientY - dialogHeaderEl.offsetTop;// 获取到的值带px 正则匹配替换let styL, styT;// 注意在ie中 第一次获取到的值为组件自带50% 移动之后赋值为pxif (sty.left.includes('%')) {styL = +document.body.clientWidth * (+sty.left.replace(/\%/g, '') / 100);styT = +document.body.clientHeight * (+sty.top.replace(/\%/g, '') / 100);} else {styL = +sty.left.replace(/\px/g, '');styT = +sty.top.replace(/\px/g, '');};// 鼠标拖拽事件document.onmousemove = function (e) {// 通过事件委托,计算移动的距离 (开始拖拽至结束拖拽的距离)const l = e.clientX - disX;const t = e.clientY - disY;let finallyL = l + styLlet finallyT = t + styT// 移动当前元素dragDom.style.left = `${finallyL}px`;dragDom.style.top = `${finallyT}px`;};document.onmouseup = function (e) {document.onmousemove = null;document.onmouseup = null;};}}
};

待续。。。

install与directive相关推荐

  1. (九)前端优化细节和额外知识点

    在这里学习到了常见的性能优化和如何将文件打包并且进行上传 性能优化包括以下部分 组件缓存 处理头像不在更新(缓存问题) 实现代码高亮 loading效果 登录未遂地址 图片懒加载 自动聚焦问题 组件注 ...

  2. 解决debian apt-get upgrade 错误triggers ci file contains unknown directive `interest-noawait'

    错误信息如下: Errors were encountered while processing:  /var/cache/apt/archives/mime-support_3.54_all.deb ...

  3. Nginx配置报错unknown directive echo的解决

    Nginx配置报错unknown directive "echo"的解决 实际上,Nginx并没有内置echo这个指令,所以你贸然使用时,自然会提示说无法识别的指令.它是由agen ...

  4. vue从入门到进阶:自定义指令directive,插件的封装以及混合mixins(七)

    一.自定义指令directive 除了核心功能默认内置的指令 (v-model 和 v-show),Vue 也允许注册自定义指令.注意,在 Vue2.0 中,代码复用和抽象的主要形式是组件.然而,有的 ...

  5. 【vue开发】vue插件的install方法

    MyPlugin.install = function (Vue, options) {// 1. 添加全局方法或属性Vue.myGlobalMethod = function () {// 逻辑.. ...

  6. How to Install and Configure OpenSSH Server In Linux

    标题:在Linux中安装和配置OpenSSH服务器 Install OpenSSH in Linux  & 在Linux计算机中安装OpenSSH Being a network admini ...

  7. yolov5安装pip install requirements.txt,pycocotools安装报错

    项目场景: 系统:ubuntu16.04 安装yolov5,直接pip install requirements.txt,报错 问题描述: 报错信息如下,可见是pycocotools安装错误 Buil ...

  8. php warning: directive,安装Composer PHP Warning: copy(): SSL operation failed with code

    报错信息 [root@localhost ~]# php -r "copy('https://install.phpcomposer.com/installer', 'composer-se ...

  9. Windows安装Apache(解决问题Set the 'ServerName' directive globally to suppress this message)

    下载Apache 修改tttp.conf文件 命令安装http服务 (解决问题Could not reliably determine the server's fully qualified dom ...

最新文章

  1. 【AtCoder - 4244 】AtCoder Express 2 (区间dp 或 暴力枚举,思维)
  2. oracle form中实现隐藏,Oracle Form数据块实现同时只有一个人锁定修改数据
  3. 回顾2017年JavaScript状况
  4. 7-53 字符串逆序 (10 分)
  5. .net mysql 工作流_一个适合于.NET Core的超轻量级工作流引擎:Workflow-Core
  6. 在 MacBook 中如何将外置屏幕设置为主屏幕?
  7. 移动端实现摇一摇并振动
  8. Equinox 和 OSGI 介绍
  9. 中国20年互联网的发展史
  10. win10系统启动wifi服务器,windows10系统下开启wifi共享的两种方法
  11. 怎么通过网络快速赚钱,无非是这4种方式!
  12. 读书笔记-干法-人生三毒
  13. 接口测试,后端接口还没开发完,如何测?解决看这一篇就够了......
  14. 对接京东获取任务工单接口jingdong.homefw.task.search,附接口字段详细说明,数据库设计,Java实现
  15. 凉凉夜色为你思念成河
  16. 深入理解搜索引擎-搜索召回
  17. 采油工计算机试题库,采油模拟试题集1
  18. Switchbox系列 - Windows安装和使用
  19. office2021与365你还在纠结吗?快来看看
  20. ios11.2计算机更新,iOS11.2.2正式版怎么样、更新了什么?iOS11.2.2降频吗?

热门文章

  1. 如何设计主图能提升点击率?
  2. (六)play之yabe项目【验证码】
  3. 学习java第三天,今天是数据类型和运算符(2)
  4. 硬盘分区2----GPT与MBR的区别
  5. 360p1和水星mercury两台路由器怎么设置中继上网步骤方法
  6. android10怎么恢复出厂设置,手机恢复出厂设置,小编告诉你安卓手机怎么恢复出厂设置...
  7. 机器学习实战(十)——利用K-均值聚类算法对未标注数据分组
  8. 十一假期前一定要看的一篇文
  9. 赛百味签订新特许经营协议,20年内在中国大陆开设近4000家餐厅
  10. 港联证券|北交所行情活跃 近九成个股5月以来录得上涨