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

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

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

过滤器基本介绍

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

全局过滤器

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

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

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

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

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

<div id="app">
    <p>{
   
   {msg | msgFormat('极了','!')}}</p>
</div>
<script>
    Vue.filter('msgFormat',function(msg,arg1,arg2){
     
     
        console.log(msg);
        return msg.replace(/好/g,arg1+arg2);
    })


    new Vue({
     
     
        el:'#app',
        data:{
     
     
            msg:'你好,我好,大家好!'
        }
    })
</script>

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

过滤器可以串联

{ { message | filterA | filterB }}

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

用法:

<div id="app">
    <p>{
   
   {msg | msgFormat('极了','!')| test}}</p>
</div>
<script>
    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:'你好,我好,大家好!'
        }
    })
</script>

在这里插入图片描述

局部过滤器

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

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

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

用法:转换时间格式

<div id="app">
    <p>这是使用过滤器的时间格式:{
   
   {dt | dateFormat}}</p>
    <p>这是原始格式:{
   
   {dt}}</p>
</div>
<script>
    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}`
            }
        }

    })
</script>

在这里插入图片描述

Vue.directive

自定义全局指令

语法结构

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

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

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

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

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

用法

<div id="app">
   <input type="text" name="" v-focus id="">
   <p v-color>这是一个p标签</p>
</div>
<script>
   // 定义全局的自定义指令  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'
   })
</script>

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

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

<p v-color="'pink'">这是一个p标签</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(){
    
    
			}
    	}
    }
})

用法

<div id="app">
    <p>这是一个p段落标签</p>
    <p v-fontweight="900" v-fontsize="50">这是一个p段落标签</p>
</div>
<script>
    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'
            }
        }
    })
</script>

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/PILIpilipala/article/details/109584920