install与directive

2023-05-16

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 } = binding
    const all_permission = "*:*:*";
    //const permissions = store.getters && store.getters.permissions // 登录后获取的当前登录用户的权限字符数组
    const permissions = ["but"]  
    if (value && value instanceof Array && value.length > 0) {
      const permissionFlag = value
      const 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.value
      if (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% 移动之后赋值为px
         if (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 + styL
            let finallyT = t + styT

            // 移动当前元素
            dragDom.style.left = `${finallyL}px`;
            dragDom.style.top = `${finallyT}px`;

         };

         document.onmouseup = function (e) {
            document.onmousemove = null;
            document.onmouseup = null;
         };
      }
   }
};

待续。。。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

install与directive 的相关文章

随机推荐

  • {}与Object.create(null)

    var one 61 创建的对象带有 proto 下面有一些方法与属性 xff0c 这便是js的原型链继承 xff0c 继承了object的方法和属性 xff1b 故在遍历对象时 xff0c 会遍历原型链上的属性 xff0c 带来性能上的损
  • uni-app 全局变量机制

    getApp globalData 全局变量机制 在App vue中 export default globalData text 39 text 39 在App vue中调用 this globalData text在onLaunch生命
  • vue3动态注册路由

    在vue cil2中 xff0c 我们可以通过webpack中require context这个api实现工程自动化 xff0c 而在vue cil3里vite替代了webpack xff0c 节省了webpack冗长的打包时间的同时我们也
  • try{}catch(res){}、throw(exception)、new Error()

    1 try catch res try 中的代码出现错误异常时 xff0c 系统会将异常信息封装到error对象中 xff0c 传递给catch res xff0c 包含res message res name等 EvalError eva
  • new Map()

    1 new Map let data 61 new Map data set key value 添加一个新建元素到映射 Map 1 key 61 gt value data get key 返回映射中的指定元素 data has key
  • Proxy代理

    Proxy用于修改某些操作的默认行为 xff0c 等同于在语言层面做出修改 xff0c 所以属于一种 元编程 语法 xff1a let proxy 61 new Proxy target handler target 所要拦截的目标对象ha
  • Jmeter性能测试(7)--定时器

    jmeter xff08 七 xff09 定时器 jmeter提供了很多元件 xff0c 帮助我们更好的完成各种场景的性能测试 xff0c 其中 xff0c 定时器 xff08 timer xff09 是很重要的一个元件 xff0c 最新的
  • oninput完美限制输入正整数

    oninput完美限制输入非0正整数 注意vue中需要 64 input进行绑定 方法一 64 input 61 34 if this value length 61 61 1 this value 61 this value replac
  • 行内存放数据属性data-id

    data 61 39 data 39 为行内存放数据的属性 xff0c 可通过事件源中的currentTarget dataset获取data 存放的值 另外css可通过 data 放置的标签名 data 61 39 data 39 设置
  • js常用封装方法

    span class token comment 生成随机数 64 length 指定长度 return 随机数 span span class token keyword export span function span class t
  • 计数器组件

    涉及事件 64 longpress 长按时触发 xff0c 64 touchend 手指从屏幕上离开时触发 1 计数器为文本标签的子组件 lt template gt lt view class 61 34 counter box 34 g
  • rich-text 富文本

    rich text 富文本 普通的text文件不能显示格式 xff0c 富文本格式rtf文件可以显示出很多格式信息 xff0c 比如可以在一个文本包含不同颜色 不同字号的文本 官方 lt rich text nodes 61 34 cont
  • uni-app实现全局组件注册

    uni app 全局注册组件三种方式 1 传统vue组件需要创建 引用 组成三个步骤 2 在page json中对应page设置 34 globalStyle 34 34 autoscan 34 true 和pages同级 3 HBuild
  • Vue--混入(Mixin)

    Vue 混入 Mixin 当不同组件有相同功能时 xff0c 不必重复定义属性和方法 xff0c 可使用vue中的混入 Mixin 来分发 Vue 组件中的可复用功能 一个 mixin 对象可以包含任意组件选项 xff0c 即data me
  • uni-app--tabs切换swiper

    父组件 span class token operator lt span template span class token operator gt span span class token operator lt span view
  • 关于移动端 html5诸多事件

    1 点击事件 64 click与 64 tap的区别 xff1a 64 click 在web手机端上点击 xff0c 有300ms延迟再被触发 64 tap具有事件穿透特点 而 64 click没有 事件冒泡 xff1a 当父元素有点击事件
  • vuex状态管理

    vue 1 下载vuex依赖 2 创建store目录store js xff0c 然后在js中引入 span class token keyword import span span class token module Vue span
  • Jmeter性能测试(8)--断言

    jmeter xff08 8 xff09 断言 jmeter中有个元件叫做断言 xff08 Assertion xff09 xff0c 它的作用和loadrunner中的检查点类似 xff1b 用于检查测试中得到的响应数据等是否符合预期 x
  • Class类

    class类的基本写法 es6引入了class类的概念 xff0c 可通过class关键字来定义类每个类都会有一个构造函数 xff0c 即constructor 方法 xff0c 用于创建和初始化class对象要注意 xff0c 如果一个类
  • install与directive

    install Vue option Vue js提供install方法 xff0c 可用于开发新插件以及全局注册组件等 span class token keyword export span span class token keywo