网站首页 > 博客文章 正文
在Vue中,指令都是以 v-xxx 命令,因此自定义指令也是如此
自定义指令时,指令名称不需要加前缀v-,但在调用的时候一定要加前缀v-
自定义一个获取焦点的指令 v-focus
//定义一个全局的指令(directive:指令)
//参数1为指令名称,定义的时候,不需要加 v- ,调用时需要v- ,
//参数2是对象,对象中有指令相关函数,在特定时候运行,
Vue.directive('focus',{
//获取当前指令(v-focus)绑定的元素e,e.focus()方法在绑定元素插入到DOM中时调用
inserted:function(e){
e.focus()
}
})
//等同于js中: var e = document.getElementById("e")
// e.focus()
//局部指令定义,vm实例中
directives:{
focus:{
inserted:function(e){
e.focus()
}
}
}
钩子函数:
- bind:只调用一次,第一次绑到到元素时调用,一般用在初始化中,在inserted之前,此时父节点为null,在dom树绘制之前调用,一般用作初始化属性(样式、元素属性)的设置,绑定后属性就已经在当前元素中了。
- inserted:被绑定元素插入到父节点时调用,父节点存在即可,只调用一次,在dom树绘制之后调用,一般用作初始化方法(js的行为方法)的设置,绑定到DOM中后立即执行一次。
- updated:所在的组件更新时调用,在componentUpdated之前调用,更新前的状态,多次调用
- componentUpdated:所在的组件及及子元素全部更新时调用,更新后的状态,多次调用
- unbind:只调用一次,指令与元素解绑时调用
Vue.directive('指令名称',{
//指令绑定到元素上的时候会立即执行,一般用作初始化,只执行一次,当指令刚被绑定到元素的时候,还没有插入到DOM树中去
//绑定时不管有没有插入到DOM中,已经有了value="" 和 style="color:red"的属性
//bind因此可以做一些初始化属性的设置
bind:function (e) {
e.value = "文本框初始值"
e.style.color = 'red'
},
inserted:function (e) { //元素插入到DOM树中后会立即执行一次,可作初始化方法的设置
e.focus()
},
updated:function (e) { //vnode更新的时候会执行,可触发多次
},
componentUpdated:function (e) {
},
unbind:function (e) {
}
})
<input type="text" v-focus>
钩子函数参数:
- el:指令被绑定的当前元素,即当前指令所在的元素,此参数名可以变更,其他参数名不能变更
- binding:
value:指令绑定值,v-color=" 'blue' ","1+1"为2
arg:传递给指令的参数v-color:blue
modifiers:包含修饰符对象 v-mydirective.foo.bar modifiers为{foo.true,bar,true}
3.vnode:vue编译器生成的虚拟节点
4.oldVnode:上一个虚拟节点
Vue.directive("color",{
bind:function (e,binding) {
e.style.color = binding.value
e.style.color = binding.arg
}
})
//注意单引号,否则blue为data中数据
<input type="text" v-focus v-color="'blue'">
<input type="text" v-focus v-color:blue>
自定义指令的简写
如果只有bind或updated钩子函数,则可以简写
Vue.directive("color", (e,binding) => {
e.style.color = binding.value
e.style.color = binding.arg
})
//注意单引号,否则blue为data中数据
<input type="text" v-focus v-color="'blue'">
<input type="text" v-focus v-color:blue>
猜你喜欢
- 2024-10-09 9、Vue生命周期(vue生命周期的四个阶段)
- 2024-10-09 「前端Vue学习系列」三、Vue进阶篇
- 2024-10-09 面试官:你对vue生命周期的理解?(简述vue生命周期)
- 2024-10-09 vue的生命周期(概览)(vue生命周期的四个阶段)
- 2024-10-09 Vue如何创建自定义指令?(vuecli新增自定义指令)
- 2024-10-09 Vue-生命周期总结(vue中的生命周期)
- 2024-10-09 vue的生命周期(vue 的生命周期)
- 2024-10-09 vue3讲解setup,ref,reactive和watch语法
- 2024-10-09 vue基础知识(vue要掌握哪些知识?)
- 2024-10-09 Vue3学习手册(vue3 从入门到实战)
你 发表评论:
欢迎- 07-07Xiaomi Enters SUV Market with YU7 Launch, Targeting Tesla with Bold Pricing and High-Tech Features
- 07-07Black Sesame Maps Expansion Into Robotics With New Edge AI Strategy
- 07-07Wuhan's 'Black Tech' Powers China's Cross-Border Push with Niche Electronics and Scientific Firepower
- 07-07Maven 干货 全篇共:28232 字。预计阅读时间:110 分钟。建议收藏!
- 07-07IT运维必会的30个工具(it运维工具软件)
- 07-07开源项目有你需要的吗?(开源项目什么意思)
- 07-07自动化测试早就跑起来了,为什么测试管理还像在走路?
- 07-07Cursor 最强竞争对手来了,专治复杂大项目,免费一个月
- 最近发表
-
- Xiaomi Enters SUV Market with YU7 Launch, Targeting Tesla with Bold Pricing and High-Tech Features
- Black Sesame Maps Expansion Into Robotics With New Edge AI Strategy
- Wuhan's 'Black Tech' Powers China's Cross-Border Push with Niche Electronics and Scientific Firepower
- Maven 干货 全篇共:28232 字。预计阅读时间:110 分钟。建议收藏!
- IT运维必会的30个工具(it运维工具软件)
- 开源项目有你需要的吗?(开源项目什么意思)
- 自动化测试早就跑起来了,为什么测试管理还像在走路?
- Cursor 最强竞争对手来了,专治复杂大项目,免费一个月
- Cursor 太贵?这套「Cline+OpenRouter+Deepseek+Trae」组合拳更香
- 为什么没人真的用好RAG,坑都在哪里? 谈谈RAG技术架构的演进方向
- 标签列表
-
- ifneq (61)
- 字符串长度在线 (61)
- messagesource (56)
- aspose.pdf破解版 (56)
- promise.race (63)
- 2019cad序列号和密钥激活码 (62)
- window.performance (66)
- qt删除文件夹 (72)
- mysqlcaching_sha2_password (64)
- ubuntu升级gcc (58)
- nacos启动失败 (64)
- ssh-add (70)
- jwt漏洞 (58)
- macos14下载 (58)
- yarnnode (62)
- abstractqueuedsynchronizer (64)
- source~/.bashrc没有那个文件或目录 (65)
- springboot整合activiti工作流 (70)
- jmeter插件下载 (61)
- 抓包分析 (60)
- idea创建mavenweb项目 (65)
- vue回到顶部 (57)
- qcombobox样式表 (68)
- tomcatundertow (58)
- pastemac (61)
本文暂时没有评论,来添加一个吧(●'◡'●)