vue.js 自定义指令

钩子函数:
bind
inserted
update
componentUpdated
unbind

钩子函数完整实例:
html:

<div id="hook-arguments-example"
     v-demo:hello.a.b="message"
     v-color-swatch="bgColor"
        ></div>

js:

Vue.directive('demo', {
    bind: function (el, binding, vnode) {
        var s = JSON.stringify; // 用于从一个对象解析出字符串
        el.innerHTML =
            'name: ' + s(binding.name) + '<br>'
            +'value: ' + s(binding.value) + '<br>'
            +'expression: ' + s(binding.expression) + '<br>'
            +'argument: ' + s(binding.arg) + '<br>'
            +'modifiers: ' + s(binding.modifiers) + '<br>'
            + 'vnode keys: ' + Object.keys(vnode).join(', ')
    }
});

Vue.directive('color-swatch', function (el, binding) {
    el.style.backgroundColor = binding.value;
});

new Vue({
    el: '#hook-arguments-example',
    data: {
        message: 'hello!',
        bgColor: 'red'
    }
});

结果:

Vue混合:

// 定义一个混合对象
var mixin = {
    methods: {
        foo: function () {
            console.log('foo');
        },
        conflicting: function () {
            console.log('from mixin');
        }
    }
};

var vm = new Vue({
    mixins: [mixin],
    methods: {
        bar: function () {
            console.log('bar');
        },
        conflicting: function () {
            console.log('from self');
        }
    }
});

vm.foo();
vm.bar();
vm.conflicting();

最新文章

  1. Kooboo CMS 之TextContent详解
  2. sudo su权限案例
  3. NBU bplabel命令擦除磁帶數據
  4. [C程序设计语言]第四部分
  5. python内存管理机制
  6. 看完com本质论第一章
  7. linux PCI设备初始化过程
  8. C语言清空输入缓冲区的N种方法对比
  9. innode 节点
  10. Python 基本数据类型_常用功能整理
  11. Plupload上传插件自定义图片的修改
  12. MyBatis 关系映射XML配置
  13. linux命令详解:pgrep命令
  14. 关于C#中的++运算符的一些拓展思考
  15. python 机器学习
  16. Servelet开发步骤和生命周期
  17. 散列之HashTable学习
  18. Fiddler抓包7-post请求(json)
  19. spring aop通过注解实现日志记录
  20. Redis学习笔记(1)- CentOS 6.4 安装Redis

热门文章

  1. html特殊字符转义问题(转!)
  2. 交换机和VLAN
  3. js 去掉字符串前后空格
  4. AIX网络性能优化简介
  5. CMMI整体理解
  6. JS延时提示框
  7. 找出单链表的倒数第K个(从1开始计数)结点的值
  8. tcp/ip的一些概念
  9. Web大文件下载控件更新-Xproer.HttpDownloader
  10. 「2013-9-5」Configure WingIDE for better display of East Asian Glyphs