vue 自定义指令的使用案例

参考资料:

1. vue 自定义指令

2. vue 自定义指令实现 v-loading

 

  v-loading,是 element-ui 组件库中的一个用于数据加载过程中的过渡动画指令,项目中也很少需要自己去写自定以指令。碰巧这段时间自己练习了编写组件,完成看看能不能实现一个自定义的这样一个指令。话不多说,上代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<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.关于指令的钩子函数以及相关参数,具体移步 官方文档

 

posted @   shiweiqianju  阅读(762)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 25岁的心里话
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示