vue directives 使用详解 |
您所在的位置:网站首页 › js中focus用法 › vue directives 使用详解 |
自定义指令钩子的执行顺序
点击 codepen 查看指令钩子函数的执行顺序 created() { console.log('created') }, mounted() { console.log('mounted') }, directives:{ focus:{ inserted(el){ console.log('inserted-el',el) }, bind(el) { console.log('bind-el',el) }, update(el) { console.log('directives-update',el) } } }, updated() { console.log('updated') },当我们加载页面的时候执行顺序 created bind-el inserted-el mounted当我们更新 input 输入框的值的时候 directives-update updated 钩子函数参数 绑定后可以拿到的参数 directives:{ focus:{ inserted(el,binding){ console.log('inserted-el',el,binding) } } }格式: v-focus:arg, arg 传递的参数会被当作字符串类型的,通过 binding.arg 接收 directives:{ focus:{ inserted(el,binding){ console.log('inserted-el',binding) } } }格式: v-focus="value" ,这里的 value 是变量名,而不是字符串。 data() { return { value:'', format1:'date' } },可以看到,通过绑定 value ,指令中接收的 binding 多了个 value 的属性,值就我们传递进来的变量值。而且还多了一个 expression ,expression:字符串形式的指令表达式。例如 v-my-directive="1 + 1" 中,表达式为 "1 + 1"。 修饰符格式: v-focus.isFocus, 修饰符对象为: {isFocus:true} data() { return { value:'' } },该指令有如下功能 可以设置保留几位小数 可以设置格式化形式:整数、金额类每三位一个逗号。并且使用适配的模式,后序要新增其他模式也是很方便不会影响到之前的代码逻辑点击 codepen 地址查看:数字格式化指令的完整代码 |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |