vue directive 指令开发
为什么要写指令? directive 和 component 的区别是什么? 指令适合处理简单的数据和对DOM的一些操作,v-focus,v-touch这种行为的一些操作适合用指令去实现。 涉及到复杂的行为,尤其是要接受各种选项的,那么就适合在 component 中去实现 https://github.com/vuejs/vue/issues/862 全局指令 // 注册一个全局自定义指令 `v-focus` Vue.directive(‘focus’, { // 当被绑定的元素插入到 DOM 中时…… inserted: function (el) { // 聚焦元素 el.focus() } }) 局部指令 组件中设置一个 directives 的选项 directives: { focus: { // 指令的定义 inserted: function (el) { el.focus() } } } 全局指令插件写法 !function ()…