vue 自定义指令 v-name
除了核心功能默认内置的指令 (v-model 和 v-show),Vue 也允许注册自定义指令。有的情况下,对普通 DOM 元素进行底层操作,这时候就会用到自定义指令
在main.js中定义
// name为指令名 用法v-name
Vue.directive('name', {
inserted: function (el) {
// el 表示指令所绑定的元素 判断类型
el.value = Number(el.value)
}
});
//focus为指令名 用法v-focus
Vue.directive('focus', {
inserted: function (el) {
// el 表示指令所绑定的元素 给事件
el.focus()
}
});
//focus为指令名 用法v-displayNone
Vue.directive('displayNone', {
inserted: function (el) {
// el 表示指令所绑定的元素 判断给样式
if (store.state.userData.profile.userUserTypeId !== 1) {
el.style = 'display: none;'
}
}
});
你也可以输入一下el 看看
看得到标签全部属性都能拿到
使用方式
直接绑定即可