首页 / VUE / vue 自定义指令的使用案例
vue 自定义指令的使用案例
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了vue 自定义指令的使用案例,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2151字,纯文字阅读大概需要4分钟。
内容图文
![vue 自定义指令的使用案例](/upload/InfoBanner/zyjiaocheng/1290/d46deaa8e510462d831d53b173e77be0.jpg)
参考资料:
1. vue 自定义指令;
v-loading,是 element-ui 组件库中的一个用于数据加载过程中的过渡动画指令,项目中也很少需要自己去写自定以指令。碰巧这段时间自己练习了编写组件,完成看看能不能实现一个自定义的这样一个指令。话不多说,上代码:
<div class="table" v-loadAnimation="loading"> ... </div> <script> export default { data() { return { ... } }, directives: { loadAnimation: { bind: (el, binding) => { // console.log(‘bind‘, el, binding) // 遮罩层 const modal = document.createElement(‘div‘); modal.className = ‘modal-loading‘; // 遮罩层动画 const animation = document.createElement(‘div‘); animation.className = ‘modal-loading-animation‘; modal.appendChild(animation); // 自定义的 loadingElement 属性/其他, 下面钩子函数可以使用; el.loadingElement = modal; const curStyle = window.getComputedStyle(el); const position = curStyle.position; if (position === ‘absolute‘ || position === ‘relative‘) { el.style.position = position; } else { el.style.position = ‘relative‘; } if (binding.value) { el.appendChild(modal) } }, update: (el, binding) => { // console.log(‘update‘, el, binding) if (binding.value) { if (el.loadingElement.parentNode === null) { el.appendChild(el.loadingElement); } } else { if (el === el.loadingElement.parentNode) { el.removeChild(el.loadingElement); } } }, unbind: (el) => { if (el.loadingElement.parentNode === el) { el.removeChild(el.loadingElement); } el.loadingElement = null; } } }, methods: { ... } } </script> <style> /* for loading animation */ .modal-loading { width: 100%; height: 100%; position: absolute; left: 0; top: 0; z-index: 100; background-color: rgba(255, 255, 255, 0.95); overflow: hidden; } .modal-loading .modal-loading-animation { width: 30px; height: 30px; border-radius: 50%; border: 4px solid #1a7dff; border-left-color: transparent; animation: modal-loading-rotate 1s linear infinite; position: absolute; top: 0; bottom: 0; left: 0; right: 0; margin: auto; } @keyframes modal-loading-rotate { from { transform: rotate(0deg); } to { transform: rotate(360deg); } } </style>
注意:
1.这个示例中,v-loadAnimation 是以局部指令的方式注册的,在写相关 css 的时候,style 标签中不能加入 scoped 字样,否则不会触发效果;
2.关于指令的钩子函数以及相关参数,具体移步 官方文档;
原文:https://www.cnblogs.com/cc-freiheit/p/10528923.html
内容总结
以上是互联网集市为您收集整理的vue 自定义指令的使用案例全部内容,希望文章能够帮你解决vue 自定义指令的使用案例所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。