-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path13-自定义指令.html
59 lines (55 loc) · 1.27 KB
/
13-自定义指令.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<div id="app">
<p v-text="obj"></p>
<p v-ots="obj"></p>
<p v-ots="msg"></p>
</div>
<script type="text/javascript" src="js/vue-2.1.3.js"></script>
<script type="text/javascript">
// 自定义指令object to str 变量是对象时转为字符串
Vue.directive('ots', {
bind: function (el,binding) {
console.log('demo bound!');
var str;
if (typeof binding.value === 'object') {
// 如果是对象,先转为字符 串
str = JSON.stringify(binding.value);
} else {
str = binding.value;
}
el.innerHTML = str;
},
update: function (el,binding,vnode,oldVnode) {
console.log('do nothing!');
console.log(vnode);
console.log(oldVnode);
// var str;
// if (typeof binding.value === 'object') {
// // 如果是对象,先转为字符串
// str = JSON.stringify(binding.value);
// } else {
// str = binding.value;
// }
// el.innerHTML = str;
}
})
var vm = new Vue({
el: '#app',
data: {
msg: '自定义指令ots',
obj: {
userName: '楚留香',
age: 200,
hobby: '武功'
}
}
})
</script>
</body>
</html>