vue基础--自定义指令
来源:互联网 发布:婴儿床什么牌子好 知乎 编辑:程序博客网 时间:2024/06/04 00:47
语法
// 第一个参数:指令名称// 第二个参数:配置对象,指定指令的钩子函数Vue.directive('directiveName', { bind() {},// 只调用一次,指令第一次绑定到元素时调用。 update() {},//所在组件的 VNode 更新时调用(先) componentUpdated(){},// 所在组件的 VNode 更新时调用 inserted:function(el){},//被绑定元素插入父节点时调用 unbind:function(){}//只调用一次,指令与元素解绑时调用})// 第二个参数是函数Vue.directive('red', function(el, binding) {})// 【钩子函数的参数】// el:指令所绑定的元素,可以用来直接操作 DOM // binding:一个对象// vnode:Vue 编译生成的虚拟节点// oldVnode:上一个虚拟节点,仅在 update 和 componentUpdated 钩子中可用。update(el, binding, vnode, oldVnode){binding.name;//指令名binding.value;//指令的绑定值binding.oldValue;//指令绑定的前一个值,仅在 update 和 componentUpdated 钩子中可用binding.expression;//字符串形式的指令表达式binding.arg;//传给指令的参数v-指令:foobinding.modifiers;//一个包含修饰符的对象}
使用
<p v-for="(url, index) in list" :key="index" v-img="url"></p>Vue.directive('img', { bind(el, binding) { // 设置随机颜色值 el.style.backgroundColor = '#' + parseInt(Math.random() * 1000000) // 等到图片加载完成,将当前图片设置为元素的背景 var img = new Image() // 设置图片对象的路径 img.src = binding.value // 等待图片加载完成 img.onload = function() { el.style.backgroundImage = 'url(' + img.src + ')' } } })<h1 v-指令名> </h1>
局部指令
directives: { directiveName: {钩子函数} }
阅读全文
0 0
- vue基础--自定义指令
- vue自定义指令-vue-reclick
- vue 自定义指令
- Vue自定义指令-拖拽
- Vue.js--自定义指令
- Vue自定义指令
- vue自定义指令
- vue自定义指令
- vue之自定义指令
- Vue.js--自定义指令
- vue-自定义指令-拖拽
- 14-Vue自定义指令
- Vue.directive 自定义指令
- Vue自定义指令(directive)
- Vue之自定义指令
- Vue之自定义指令
- Vue.js 自定义指令
- vue自定义指令
- springboot + swagger的实体类属性注解(一)
- js-图片缩放
- STM32项目(五)——智能回收箱
- raspberry pi zero w: yocto image 启动bluetooth
- 使用exe.4j将jar包打包为exe文件
- vue基础--自定义指令
- CSDN Markdown页面示例
- 年底别慌,没跳好槽的你还能做这些
- c语言练习题
- 一则通过Android Canvas动态绘制图形的实例介绍
- 从今天开始,每天稳定博客,学习网络安全
- 安卓7.0 状态栏分析 01----> 信号栏更新
- SDN之旅—mininet(在Windows系统上)安装
- IIS请求筛选模块被配置为拒绝包含双重转义序列的请求404.11