自定义指令
# 自定义指令
# 生命周期钩子
const myDirective = {
// 在绑定元素的 attribute 前
// 或事件监听器应用前调用
created(el, binding, vnode, prevVnode) {
// 下面会介绍各个参数的细节
},
// 在元素被插入到 DOM 前调用
beforeMount(el, binding, vnode, prevVnode) {},
// 在绑定元素的父组件
// 及他自己的所有子节点都挂载完成后调用
mounted(el, binding, vnode, prevVnode) {},
// 绑定元素的父组件更新前调用
beforeUpdate(el, binding, vnode, prevVnode) {},
// 在绑定元素的父组件
// 及他自己的所有子节点都更新后调用
updated(el, binding, vnode, prevVnode) {},
// 绑定元素的父组件卸载前调用
beforeUnmount(el, binding, vnode, prevVnode) {},
// 绑定元素的父组件卸载后调用
unmounted(el, binding, vnode, prevVnode) {}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 钩子参数
el:指令绑定到的元素。这可以用于直接操作 DOM。
binding:一个对象,包含以下属性。
value:传递给指令的值。例如在 v-my-directive="1 + 1" 中,值是 2。
oldValue:之前的值,仅在 beforeUpdate 和 updated 中可用。无论值是否更改,它都可用。
arg:传递给指令的参数 (如果有的话)。例如在 v-my-directive:foo 中,参数是 "foo"。
modifiers:一个包含修饰符的对象 (如果有的话)。例如在 v-my-directive.foo.bar 中,修饰符对象是 { foo: true, bar: true }。
instance:使用该指令的组件实例。
dir:指令的定义对象。
vnode:代表绑定元素的底层 VNode。
prevNode:之前的渲染中代表指令所绑定元素的 VNode。仅在 beforeUpdate 和 updated 钩子中可用。
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
# 使用
# 多生命周期模式
<template>
<div class="wrapper">
<header v-color="{background: 'red'}">
head
</header>
</div>
</template>
<script lang="ts" setup>
import type { Directive, DirectiveBinding } from 'vue'
interface Dcolor {
background?: string
}
const vColor:Directive = {
created: ()=> {},
beforeMount: ()=> {},
// 主要使用
mounted: (el: HTMLHeadElement, binding: DirectiveBinding<Dcolor>)=> {
el.style.backgroundColor = binding.value?.background || '#0094ff'
},
beforeUpdate: ()=> {},
// 主要使用
updated: ()=> {},
beforeUnmount: ()=> {},
// 主要使用
unmounted: ()=> {},
}
</script>
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
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
# 函数简写
即在
mounted
和updated
钩子中触发相同行为,而不关心其他的钩子函数,这种模式就是函数简写模式
示例1:
<template>
<div class="wrapper">
<button v-auth>编辑</button>
<button v-auth="'delete'">删除</button>
</div>
</template>
<script lang="ts" setup>
import type { Directive, DirectiveBinding } from 'vue'
const vAuth:Directive = (el: HTMLButtonElement, binding: DirectiveBinding<string>)=>{
if(!binding.value){
el.style.display = "none"
}
}
</script>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
示例2:图片懒加载
<template>
<div class="wrapper" v-for="item in imgSrc.src">
<img width="200" height="300" :key="item.src" v-lazy="item.src" alt="">
</div>
</template>
<script lang="ts" setup>
import type { Directive, DirectiveBinding } from 'vue'
import logo from "@/assets/logo.png"
const imgSrc = {
get src(){
return [
{ src: "https://pic.rmb.bdstatic.com/bjh/news/e0c02073e799453a340e81afb5529431.png" },
{ src: "https://pic.rmb.bdstatic.com/bjh/news/3d208450fe00aa9aabd7843b199a6c6f.png" },
{ src: "https://pic.rmb.bdstatic.com/bjh/news/8aad440068273de31257352f7037830c.jpeg" },
{ src: "https://pic.rmb.bdstatic.com/bjh/news/45d1111684eb9596e7cae713a8466df8.png" },
{ src: "https://pic.rmb.bdstatic.com/bjh/news/e57c2fe7d1edccdacd9d958971818e0e.png" },
]
}
}
const vLazy:Directive = (el: HTMLImageElement, binding: DirectiveBinding<string>)=>{
const observe = new IntersectionObserver((res)=>{
el.src = logo;
// intersectionRatio大于0,即说明出现在了可视区域了
if(res[0].intersectionRatio > 0){
setTimeout(()=>{
el.src = binding.value
}, 2000)
observe.unobserve(el)
}
})
observe.observe(el)
}
</script>
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
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
编辑 (opens new window)
上次更新: 2023/05/19, 16:45:14