zoukankan      html  css  js  c++  java
  • vue--自定义指令

    [自定义指令]:https://cn.vuejs.org/v2/guide/custom-directive.html

    1. 自定义全局和局部的 自定义指令:


      // 自定义全局指令 v-focus,为绑定的元素自动获取焦点:

      Vue.directive('focus', {

        inserted: function (el) { // inserted 表示被绑定元素插入父节点时调用

          el.focus();

        }

      });



      // 自定义局部指令 v-color 和 v-font-weight,为绑定的元素设置指定的字体颜色 和 字体粗细:

        directives: {

          color: { // 为元素设置指定的字体颜色

            bind(el, binding) {

              el.style.color = binding.value;

            }

          },

          'font-weight': function (el, binding2) { // 自定义指令的简写形式,等同于定义了 bind 和 update 两个钩子函数

            el.style.fontWeight = binding2.value;

          }

        }
    1. 自定义指令的使用方式:


    <input type="text" v-model="searchName" v-focus v-color="'red'" v-font-weight="900">

     

     

  • 相关阅读:
    48-最长不含重复字符的子字符串
    51-数组中的逆序对
    字符串的排列
    二叉树转链表
    求根
    构造二叉树
    二叉树中序遍历
    反转链表系列
    斐波那契系列
    f.lux
  • 原文地址:https://www.cnblogs.com/eadela/p/12015339.html
Copyright © 2011-2022 走看看