首页 > 其他 > 详细

vue自定义指令

时间:2018-06-02 22:41:37      阅读:225      评论:0      收藏:0      [点我收藏+]

1.main.js 文件中定义全局指令

import Vue from ‘vue‘
import App from ‘./App‘
Vue.config.productionTip = false

//定义全局指令
Vue.directive(focus‘,{
inserted:function(el){
el.focus()
}
})
/* eslint-disable no-new */
new Vue({
el: ‘#app‘,
components: { App },
template: ‘<App/>‘
})

2.页面引用和局部指令

<template>
<div>
全局指令:<input type="text" name="" id="" value="" v-focus/>
局部指令:<input type="text" name="" id="" value="" v-focus1/>
<p v-fontCss>自定义指令字体颜色</p>
</div>
</template>

<script>
export default {
name: ‘input1‘,
data() {
return {
msg: ‘子组件数据‘
}
},
directives:{
focus1:{
inserted:e1=>{
e1.focus()
}
},
fontCss:e1=>{
e1.style.color=‘red‘
}
}
}
</script>

<style>

</style>

vue自定义指令

原文:https://www.cnblogs.com/qiyc/p/9127165.html

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