vue自定义指令的创建和使用
一、自定义指令的创建和使用
Vue自带的指令很多,v-for/v-if/v-else/v-else-if/v-model/v-bind/v-on/v-show/v-html/v-text...
但是这些指令都是比较偏向于工具化,有些时候在实现具体的业务逻辑的时候,发现不够用,如何来自定义指令.
1、自定义指令
①创建
new Vue({
directives:{
change:{
bind:function(){},
update:function(){},
unbind:function(){}
}
}
})
在自定义指令时,在指令对应的配置对象中有3个处理函数指令对应的操作
bind
指令在绑定到元素要执行的操作
update
如果在调用指令时候,传了参数,当参数变化时候,就会执行update所指定的方法
unbind
解绑要执行的操作
②使用自定义指令
directives:{
hello:{
bind:function(){},
update:function(){},
unbind:function(){}
}
}
使用:
v-hello
注意事项:
建议在给指令的命名采用小驼峰式的命名方式,比如changeBackgroundColor,在使用的时候,采用烤串式写法 v-change-background-color
(方法:参数,返回值)
bind方法以及update方法 都是有参数的,
一个是el,对应的是调用指令的元素
一个bindings,是一个对象:name/rawName/value/oldValue...
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<script src="js/vue.js"></script>
<title></title>
</head>
<body> <div id="container">
<p>{{msg}}</p>
<!-- 准备实现需求:
在h1标签上面,加上一个按钮,当点击按钮时候,对count实现一次
自增操作,当count等于5的时候,在控制台输出‘it is a test’
-->
<button @click="handleClick">clickMe</button>
<h1
v-if="count < 6"
v-change="count">it is a custom directive</h1>
</div> <script>
//directive
new Vue({
el: '#container',
data: {
msg: 'Hello Vue',
count:0
},
methods:{
handleClick: function () {
//按钮单击,count自增
this.count++;
}
},
directives:{
change:{
bind: function (el,bindings) {
console.log('指令已经绑定到元素了');
console.log(el);
console.log(bindings);
//准备将传递来的参数
// 显示在调用该指令的元素的innerHTML
el.innerHTML = bindings.value;
},
update: function (el,bindings) {
console.log('指令的数据有所变化');
console.log(el);
console.log(bindings);
el.innerHTML = bindings.value;
if(bindings.value == 5)
{
console.log(' it is a test');
}
},
unbind: function () {
console.log('解除绑定了');
}
}
}
})
</script> </body>
</html>
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<script src="js/vue.js"></script>
<title></title>
</head>
<body> <div id="container">
<p>{{msg}}</p>
<h1 v-change-background-color="myBgColor">
it is a header1
</h1>
</div> <script>
new Vue({
el: '#container',
data: {
msg: 'Hello Vue',
myBgColor:'#ff0000'
},
directives:{
changeBackgroundColor:{
bind: function (el,bindings) {
console.log('in bind ');
console.log(bindings.value);
el.style.backgroundColor = bindings.value;
}
}
}
})
</script> </body>
</html>
<h4 v-change-background-color="'red'">背景色</h4>这样也是可以的,但是写死了,不好
最新文章
- 常用数据结构-线性表及Java 动态数组 深究
- Java 8 Stream API详解--转
- c++继承,多态,重载的作用
- getline()函数
- python核心编程学习记录之Python对象
- Java之美[从菜鸟到高手演练]之JDK动态代理的实现及原理
- memcached学习(4). memcached的分布式算法
- android之TextView
- Simple XML
- 知道创宇爬虫题--代码持续更新中 - littlethunder的专栏 - 博客频道 - CSDN.NET
- (原创)googlemap开发(一)
- java字符串数组进行大小排序
- spring mvc实现ajax 分页
- Yii --Command 任务处理
- ANDROID PAD版本号 PHONE版本号 源代码有什么 差别?
- 编译安装SVN不结合apache
- Codeforces348C - Subset Sums
- 详解EBS接口开发之应收款处理
- ViewPager + TabLayout + Fragment + MediaPlayer的使用
- MFC开发(一)简单同步时间应用程序
热门文章
- Codeforces 979D (STL set)(不用Trie简单AC)
- mysql-介绍、下载安装以及软件基本管理
- 问题 B: 傻鸡抓大闸蟹
- P3379 【模板】最近公共祖先(LCA)(欧拉序+rmq)
- 《A chorus section detection method for musical audio signals and its application to a music listening section》
- Flutter 初探 -
- 【推荐系统】知乎live入门4.排序
- ElasticSearch 入门介绍
- 一、模型验证CoreWebApi 管道方式(非过滤器处理)
- 吉首大学2019年程序设计竞赛(重现赛)I 滑稽树上滑稽果 (莫队+逆元打表)