-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvue中的自定义指令2.html
68 lines (58 loc) · 1.79 KB
/
vue中的自定义指令2.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="https://unpkg.com/vue@next"></script>
</head>
<style>
.header {
position: absolute;
}
</style>
<body>
<p>实现自定义属性 v-pos="数据" 使其数据可以改变dom元素的position距离</p>
<p>v-abs:right="" 绑定组件属性赋值right 用binding.arg调用</p>
<div id="root"></div>
<script>
const app = Vue.createApp({
data() {
return {
top: 100,
distance: 200
}
},
template:
`
<div>
<div v-pos="top" class="header">
<input />
</div>
<div v-abs:right="distance" class="header">
<input />
</div>
</div>
`
});
// 这种写法等价于下面的写法 即当组件中只有mounted和updated时
// app.directive('pos', () => {
// el.style.top = binding.value + 'px';
// })
app.directive('pos', {
mounted(el, binding) {
el.style.top = binding.value + 'px';
},
// 在改变数据时生效
updated(el, binding) {
el.style.top = binding.value + 'px';
}
});
app.directive('abs', (el, binding) => {
el.style[binding.arg] = (binding.value + 'px');
})
const vm = app.mount('#root');
</script>
</body>
</html>