zoukankan      html  css  js  c++  java
  • vue2 + typescript2 自定义过滤器

    1.定义一个过滤器

    // color-directive.ts
    
    import { DirectiveOptions } from 'vue'
    
    const directive: DirectiveOptions = {
        inserted(el, node) {
            /**
             * Using value:
             * v-colorDirective={color: 'red', backgroundColor: 'blue'}
             */
            if (node.value) {
                el.style.backgroundColor = node.value.backgroundColor;
                el.style.color = node.value.color;
            }
    
            /**Using modifiers:
             * v-colorDirective.color
             * v-colorDirective.backgroundColor
             */
            if (node.modifiers.color){
                el.style.color = node.value;
            }
    
            if (node.modifiers.backgroundColor) {
                el.style.backgroundColor = node.value;
            }
        }
    };
    
    export default directive;
    

    2.使用

    <template>
      <div class="hello">
        <h1 v-colorDirective="{color: 'red', backgroundColor: 'blue'}">{{ message }}</h1>
        <button @click="clicked">Click</button>
        <router-link to="/hello-ts">Hello Ts</router-link>
      </div>
    </template>
    
    <script lang="ts">
    import Vue from 'vue'
    import Component from 'vue-class-component'
    import colorDirective from '../color-directive';
    
    @Component({
      directives: {
        colorDirective
      }
    })
    export default class Hello extends Vue {
     ....   
    }
    
    <template>
        <div>
            <h3 v-colorDirective.color="'green'">HelloTs</h3>
            <router-link to="/">Hello</router-link>
        </div>
    </template>
    <script lang="ts">
    
    import Vue from 'vue'
    import Component from 'vue-class-component'
    import colorDirective from '../color-directive';
    
    @Component({
        directives: {
            colorDirective
        }
    })
    export default class HelloTs extends Vue {
      ...
    }
    

    .

  • 相关阅读:
    git连接远程分支
    如何找N个数中第i小的数
    DeconvNet
    深度学习中的Internal Convariate Shift (ICS)
    BA算法解决p-中位问题
    蚁群算法
    蝙蝠算法初探
    轨迹压缩之Douglas-Peucker算法之C++实现
    遗传算法---编程小试
    NYOJ 1000
  • 原文地址:https://www.cnblogs.com/crazycode2/p/7823562.html
Copyright © 2011-2022 走看看