当前位置: 首页 > news >正文

前端基础之《Vue(11)—自定义指令》

一、自定义指令

1、自己封装指令
什么是指令?指令本质上就是DOM功能的一种抽象封装。

如果有一些DOM功能经常用,但是Vue没有提供相关指令,建议自己封装。

2、自定义全局指令
使用Vue.directive('指令名', function() {})定义全局指令。

3、自定义局部指令
使用directives: {}定义局部指令,只能在当前组件中使用。

(1)使用function写法

directives: {// function写法'color': function(el, binding, vnode) {console.log('---el', el) // 指令所对应的DOM节点console.log('---binding', binding) // 绑定的值/表达式console.log('---vnode', vnode) // 当前的虚拟DOMel.style.color = binding.value}
}

接收3个参数:
el:指令所对应的DOM节点
binding:绑定的值/表达式
vnode:当前的虚拟DOM

(2)使用对象写法

directives: {// function写法'color': function(el, binding, vnode) {console.log('---el', el) // 指令所对应的DOM节点console.log('---binding', binding) // 绑定的值/表达式console.log('---vnode', vnode) // 当前的虚拟DOMel.style.color = binding.value},// 对象写法'color2': {bind(el, binding) { // 绑定cc变量的时候el.style.color = binding.value},update(el, binding) { // 修改cc变量的时候el.style.color = binding.value}}
}

还有其它的钩子。

注:function写法,等价于bind + update这两个。

二、例子代码

自己封装一个指令v-form,类似于v-model指令实现双向绑定。
必须要用虚拟DOM的上下文实现。

<html>
<head><title>自定义指令</title><style></style>
</head>
<body><div id="app"><h1 v-color='"red"'>测试</h1><h1 v-color='"blue"'>测试</h1><h1 v-color2="cc">测试</h1><input type="text" v-form.lazy="name" /></div><script src="https://cdn.jsdelivr.net/npm/vue@2.7.16/dist/vue.js"></script><script>Vue.directive ('form', {bind(el, binding, vnode) {console.log('---el', el)console.log('---binding', binding)console.log('---vnode', vnode)// 解构赋值const { lazy } = binding.modifiers// v-bind:value// 指令在绑值的时候,改上下文中的值el.value = vnode.context[binding.expression]// v-on:input// 实现双向绑定el.addEventListener(lazy?'blur':'input', function(ev){console.log('---事件', ev.target.value)vnode.context[binding.expression] = ev.target.value})},update(el, binding, vnode) {// 实现双向绑定el.value = vnode.context[binding.expression]}})const app = new Vue({data() {return {cc: "green",name: '张三'}},directives: {// function写法'color': function(el, binding, vnode) {//console.log('---el', el) // 指令所对应的DOM节点//console.log('---binding', binding) // 绑定的值/表达式//console.log('---vnode', vnode) // 当前的虚拟DOMel.style.color = binding.value},// 对象写法'color2': {bind(el, binding) { // 绑定cc变量的时候el.style.color = binding.value},update(el, binding) { // 修改cc变量的时候el.style.color = binding.value}}}})app.$mount('#app')</script></body>
</html>

相关文章:

  • 全栈国产化信创适配,构建安全可控的呼叫中心系统
  • 【安全扫描器原理】TCP/IP协议编程
  • 力扣面试150题--环形链表和两数相加
  • 【滑动窗口+哈希表/数组记录】Leetcode 438. 找到字符串中所有字母异位词
  • C语言中操作字节的某一位
  • Pandas 数据处理:长格式到宽格式的全面指南
  • 潇洒郎:ssh 连接Windows WSL2 Linux子系统 ipv6地址转发到ipv4地址上
  • SDC命令详解:使用get_cells命令进行查询
  • 数据结构------C语言经典题目(7)
  • 【沉浸式求职学习day25】【部分网络编程知识分享】【基础概念以及简单代码】
  • C语言实现贪心算法
  • PostgreSQL技术内幕29:事件触发器tag原理解析
  • 开发者专用部署工具PasteSpider的V5正式版发布啦!(202504月版),更新说明一览
  • 厚铜PCB打样全流程解析:从文件审核到可靠性测试的关键步骤
  • 华为L410上制作内网镜像模板:在客户端配置模板内容
  • 1.10软考系统架构设计师:优秀架构设计师 - 练习题附答案及超详细解析
  • Jetpack Compose 基础组件学习2.1:Surface
  • VuePress可以做什么?
  • Centos 7 ssh连接速度慢(耗时秒+)
  • 视频噪点多,如何去除画面噪点?
  • 日本大米价格连续16周上涨,再创最高纪录
  • 洗冤录·巴县档案|道咸年间一起家暴案
  • 韩国首尔江南区一大型商场起火,消防部门正在灭火中
  • 儒说︱问世间孝为何物
  • 汪东进卸任中海油董事长,深耕油气领域40余年、已临近退休
  • 新“出差三人组”亮相!神二十乘组简历来了