vue中使用自定义指令和过滤器(全局、局部)

<template>
  <div>
    <h3>博客总览</h3>
    <input type="text" v-model="searchText" placeholder="搜索">
    <div v-theme:column="'wide'" class="blogrow" v-for="blog in  filteredBlogs">
      <h4 v-titleColor>{{blog.title | to-upcase}}</h4>
      <article>{{blog.body  | snipper}}</article>
    </div>
  </div>
</template>
<script>
    export default {
        name: "show-blog",
        data(){
            return{
                blogs:[],
                searchText:''
            }
        },
        /*注册局部组件*/
        filters:{
         /* "to-upcase":function (value) {
            return value.toUpperCase();
          },*/
          toUpcase(value){
              return value.toUpperCase();
          }
        },
        directives:{
          'titleColor':{
              bind(el,binding,vnode){
                  el.style.color = 'blue'
              }
          }
        },
        created(){
            this.$http.get('././static/data.json')
            .then(
              res=>{
                  this.blogs = res.body;
              }
            )
        },
        computed:{
            filteredBlogs:function () {
              return this.blogs.filter((blog)=>{
                  return blog.title.match(this.searchText);
              })
            }
        }
    }
</script>

<style scoped>
  .blogrow{
    background-color: #efefef;
    padding: 10px;
    margin: 10px auto;
    text-align: left;
  }
  .blogrow h4{
    margin: 0 0 10px 0;
  }
</style>

以上是局部自定义指令和过滤器。

1.局部自定义指令 :directives属性titleColor是做为对象去声明,使用时直接v-titlColor即可。

 directives:{
          'titleColor':{
              bind(el,binding,vnode){
                  el.style.color = 'blue'
              }
          }
        },

2.局部自定义过滤器:使用filters属性,第一种和第二种声明方式都可以去实现。

 filters:{
         /* "to-upcase":function (value) {
            return value.toUpperCase();
          },*/
          toUpcase(value){
              return value.toUpperCase();
          }
        }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值