首页 > 其他 > 详细

Vue常用特性

时间:2020-07-08 17:23:01      阅读:86      评论:0      收藏:0      [点我收藏+]

强制转换成数值型(原本文本框是字符串)   v-model.number

去掉空格     v-model.trim
转变为change事件  失去焦点才改变值    v-model.lazy
 
自定义指令   directive
  <div id="app">
        <input type="text" v-focus>   //使用自定义指令必须加v-  ,这个绑定的input就执行里面的函数
        <input type="text">
    </div>
 Vue.directive(‘focus‘, {
            inserted: function (el) {
                el.focus();
            }
        });
带参数的自定义指令
 Vue.directive("color", {
        bind: function (el, binding) {
          console.log(binding.value.color); //blue
          console.log(binding.value); //返回 Object   里面包含color
          console.log(binding.value.name); //undefind
          el.style.backgroundColor = binding.value.color;
        }});
    Vue.directive(‘red‘,{
      bind:function(el,binding){
        console.log(binding.value.red)
        el.style.backgroundColor=binding.value.red;
      }
    });
bind 和 inserted
共同点: dom插入都会调用,bind在inserted之前 不同点: bind 时父节点为 null inserted 时父节点存在。 bind是在dom树绘制前调用,inserted在dom树绘制后调用
bind: function (el) {
console.log(el.parentNode) // null
console.log(‘bind‘) },
inserted: function (el) { console.log(el.parentNode)
// <div class="directive-box">...</div> console.log(‘inserted‘) }

Vue常用特性

原文:https://www.cnblogs.com/renshen/p/13267948.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!