vue 自定义指令的使用案例
参考资料:
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.关于指令的钩子函数以及相关参数,具体移步 官方文档;
vue 自定义指令的使用案例的更多相关文章
- vue自定义指令
Vue自定义指令: Vue.directive('myDr', function (el, binding) { el.onclick =function(){ binding.value(); } ...
- vue自定义指令(Directive中的clickoutside.js)的理解
阅读目录 vue自定义指令clickoutside.js的理解 回到顶部 vue自定义指令clickoutside.js的理解 vue自定义指令请看如下博客: vue自定义指令 一般在需要 DOM 操 ...
- Vue自定义指令报错:Failed to resolve directive: xxx
Vue自定义指令报错 Failed to resolve directive: modle 这个报错有2个原因: 1.指令单词拼错 2.Vue.directive() 这个方法没有写在 new Vue ...
- vue自定义指令clickoutside使用以及扩展用法
vue自定义指令clickoutside使用以及扩展用法 产品使用vue+element作为前端框架.在功能开发过程中,难免遇到使用element的组件没办法满足特殊的业务需要,需要对其进行定制,例如 ...
- vue自定义指令clickoutside扩展--多个元素的并集作为inside
都是个人理解,如果发现错误,恳请大家批评指正,谢谢.还有我说的会比较啰嗦,因为是以自身菜鸡水平的视角来记录学习理解的过程,见谅. 1.前言 产品使用vue+element作为前端框架.在功能开发过程中 ...
- 每个人都能实现的vue自定义指令
前文 先来bb一堆废话哈哈.. 用vue做项目也有一年多了.除了用别人的插件之外.自己也没尝试去封装指令插件之类的东西来用. 刚好最近在项目中遇到一个问题.(快速点击按钮多次触发多次绑定的方法),于是 ...
- vue自定义指令,比onerror更优雅的方式实现当图片加载失败时使用默认图,提供三种方法
首先,来看下效果图(演示一下图片正常加载与加载失败时的效果) 在线体验地址:https://hxkj.vip/demo/vueImgOnerror/ 一.常规方法解决 我们都知道,img标签支持one ...
- Vue自定义指令使用场景
当你第一次接触vue的时候,一定会使用到其中的几个指令,比如:v-if.v-for.v-bind...这些都是vue为我们写好的,用起来相当的爽.如果有些场景不满足,需要我们自己去自定义,那要怎么办呢 ...
- Vue自定义指令使用方法详解 和 使用场景
Vue自定义指令的使用,具体内容如下 1.自定义指令的语法 Vue自定义指令语法如下: Vue.directive(id, definition) 传入的两个参数,id是指指令ID,definitio ...
随机推荐
- Javascript继承3:将优点为我所有----组合式继承
//声明父类 function ParentClass(name){ //值类型公有属性 this.name = name //引用类型公有属性 this.books = ['Html'] } //父 ...
- C#面试分享:单例模式
C#面试分享:单例模式 提问1:请给出单例模式的实现: 答: public class Animal { private static Animal _instance = null; private ...
- 如何在vue单页应用中使用百度地图
作为一名开发人员,每次接到开发任务,我们首先应该先分析需求,然后再思考技术方案和解决方案.三思而后行,这是一个好的习惯. 需求:本项目是采用vue组件化开发的单页应用项目,现需要在项目中引入百度的地图 ...
- WebService访问oracle数据库本地调试
WebService访问oracle数据库本地调试-一步一个坑 上篇文章提到我们额数据库挂了,重装了数据库,然后呢我需要在本地调试WebService,看看那些数据结构缺失,迁移到新数据库中去.踩坑之 ...
- 07-Vue的基础使用
vue的介绍 前端框架和库的区别 nodejs的简单使用 vue的起步 指令系统 组件的使用 过滤器的使用 watch和computed 钩子函数 渐进式的JavaScript框架 vue react ...
- Spring 事务与脏读、不可重复读、幻读
索引: 目录索引 参看代码 GitHub: 1.Spring 事务 2.事务行为 一.Spring 事务: Spring 的事务机制是用统一的机制来处理不同数据访问技术的事务处理. Spring 的事 ...
- C# 第十版
地址: https://files.cnblogs.com/files/blogs2014/%E9%AB%98%E7%BA%A7%E7%BC%96%E7%A8%8B%28%E7%AC%AC11%E7% ...
- 常用SMTP地址
1.QQ邮箱(mail.qq.com) POP3服务器地址:pop.qq.com(端口:110) SMTP服务器地址:smtp.qq.com(端口:25) 2.搜狐邮箱(sohu.com): POP3 ...
- .net DLL版本管理
每个DLL打上版本号,方便识别维护
- Golang 学习权威网站
Golang 是一个开源的编程语言,它能让构造简单.可靠且高效的软件变得容易. Golang 是从2007年末由Robert Griesemer, Rob Pike, Ken Thompson主持开发 ...