Vue.js自定义指令过滤器(filter)及directive的基本语法和使用

您所在的位置:网站首页 vue内置过滤器 Vue.js自定义指令过滤器(filter)及directive的基本语法和使用

Vue.js自定义指令过滤器(filter)及directive的基本语法和使用

2024-06-11 01:41| 来源: 网络整理| 查看: 265

Vue全局指令 过滤器基本介绍全局过滤器局部过滤器 Vue.directive自定义全局指令自定义私有指令

除了核心功能默认内置的指令 (v-model 和 v-show),Vue 也允许注册自定义指令。下面我们介绍自定义指令Vue.filter和Vue.directive

内置指令一般作用于表单元素多一点,而我们自定的指令可以运用在所有元素中。

过滤器基本介绍 Vue.js 允许自定义过滤器,可被用于一些常见的文本格式化。过滤器可以用在两个地方:双花括号插值和 v-bind 表达式过滤器应该被添加在 JavaScript 表达式的尾部,由“管道 |”符号指示vue中的过滤器分为两种:局部过滤器和全局过滤器 全局过滤器

定义全局过滤器的语法结构

Vue.filter('过滤器的名称',function(){})

function()中的第一个参数固定为管道符前面传递过来的数据

过滤器是 JavaScript 函数,因此可以接收参数: {{ message | filterA('arg1', 'arg2') }}

filterA被定义为接收三个参数的过滤器函数。其中 message 的值作为第一个参数,普通字符串 arg1作为第二个参数,表达式 arg2的值作为第三个参数。 用法 我们将一些字符替换成其他字符,就可以使用过滤器

{{msg | msgFormat('极了','!')}} Vue.filter('msgFormat',function(msg,arg1,arg2){ console.log(msg); return msg.replace(/好/g,arg1+arg2); }) new Vue({ el:'#app', data:{ msg:'你好,我好,大家好!' } })

{{msg | msgFormat}}意思为msg通过管道符流入自定义的msgFormat过滤器中,function中就可以获取msg 在这里插入图片描述

过滤器可以串联

{{ message | filterA | filterB }}

filterA被定义为接收单个参数的过滤器函数,表达式 message 的值将作为参数传入到函数中。然后继续调用同样被定义为接收单个参数的过滤器函数 filterB,将 filterA的结果传递到filterB中。

用法:

{{msg | msgFormat('极了','!')| test}} Vue.filter('msgFormat',function(msg,arg1,arg2){ console.log(msg); return msg.replace(/好/g,arg1+arg2); }) Vue.filter('test',function(msg){ return msg+'===========' }) new Vue({ el:'#app', data:{ msg:'你好,我好,大家好!' } })

在这里插入图片描述

局部过滤器

定义局部过滤器的语法结构

vue提供filters属性来定义私有的过滤器

filters:{过滤器的名称:function(){}}

用法:转换时间格式

这是使用过滤器的时间格式:{{dt | dateFormat}} 这是原始格式:{{dt}} var vm=new Vue({ el:'#app', data:{ dt:new Date() }, methods: { }, filters:{ dateFormat:function(dataStr){ console.log(dataStr); var dt=new Date(dataStr); // yyyy-mm-dd var y=dt.getFullYear(); var m=(dt.getMonth()+1).toString().padStart(2,'0'); var d=dt.getDate().toString().padStart(2,'0'); var hh=dt.getHours().toString().padStart(2,'0'); var mm=dt.getMinutes().toString().padStart(2,'0'); var ss=dt.getSeconds().toString().padStart(2,'0'); return `${y}年${m}月${d}日 ${hh}:${mm}:${ss}` } } })

在这里插入图片描述

Vue.directive 自定义全局指令

语法结构:

Vue.directive('自定义指令名称',{ bind:function(){ }, inserted:function(){ }, updated:function(){ } })

自定义指令相应的用法格式为v-自定义指令名称 比如自定义的指令名称是focus,那么运用指令的时候就是v-focus

自定义指令的名称必须是小写

bind、inserted和updated在此处是固定的写法,作为钩子函数

bind:每当指令绑定到元素时,会立即调用且只调用一次。inserted:被绑定元素插入父节点时调用 函数(仅保证父节点存在,但不一定已被插入文档中)。updated:当VNode 更新数据时,会执行函数,可能会触发多次

用法:

这是一个p标签 // 定义全局的自定义指令 focus Vue.directive('focus',{ bind:function(){ }, inserted:function(el){ el.focus(); }, updated:function(){ } }) Vue.directive('color',{ bind:function(el){ el.style.color='red' } }) new Vue({ el:'#app' })

注意:在每个钩子函数中的第一个参数都是el---->表示被绑定了指令的元素 el参数是一个原生的js对象

在这里插入图片描述 优化:此时的颜色是固定’死’的,如果想要给指令单赋值去改变颜色,则需要绑定自定义指令。

这是一个p标签 Vue.directive('color',{ bind:function(el,binding){ console.log(bindin.name); el.style.color=binding.value; } })

在这里插入图片描述

自定义私有指令

与定义私有过滤器方法类似,vue提供directive属性来定义指令

语法结构:

new Vue({ el:'#app', data:{}, methods: {}, directives:{ '自定义指令名称':{ bind:function(el){ }, inserted:function(){ }, updated:function(){ } } } })

用法:

这是一个p段落标签 这是一个p段落标签 new Vue({ el:'#app', data:{}, methods: { }, // 自定义私有过滤器 filters:{}, // 自定义私有指令 directives:{ 'fontweight':{ bind:function(el,binding){ el.style.fontWeight=binding.value; } }, 'fontsize':function(el,binding){ console.log(binding); el.style.fontSize=binding.value+'px' } } })

在这里插入图片描述



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3