1、全局自定义指令。
我们定义一个自己的指令 【v-focus】获取控件的焦点。
在main.js中加入
// 注册一个全局自定义指令 【v-focus】
Vue.directive('focus', {
// 当绑定元素插入到 DOM 中。
inserted: function (el) {
// 聚焦元素
el.focus()
}
})
在TestMyComponents.vue中使用
<table>
<tr>
<td>
<p>测试全局定义的指令v-focus</p>
</td>
<td>
<input v-focus>
</td>
</tr>
</table>
测试:首次进入页面的时候输入框就会获取焦点
2、局部自定义指令
定义一个局部