Vue.js的自定义指令 directive

php中世界最好的语言
php中世界最好的语言 原创
2018-03-13 14:31:40 1451浏览

这次给大家带来Vue.js的自定义指令 directive,使用Vue.js的自定义指令directive的注意事项有哪些,下面就是实战案例,一起来看一下。

以自定义v-css指令为例:

局部指令

<script>
  export default {    //自定义v-css指令
    directives: {      css: {
        inserted (el, bind) {          let styleObj = bind.value          let arr = []          for (let key in styleObj) {
            arr.push(key + ':' + styleObj[key])
          }
          arr = arr.join(';')
          el.style.cssText = arr
        }
      }
    }</script>

全局指令
在main.js文件中自定义全局的指令

Vue.directive('css', {
  inserted (el, binding) {    let styleObj = binding.value    let arr = []    for (let key in styleObj) {
      arr.push(key + ':' + styleObj[key])
    }
    arr = arr.join(';')
    el.style.cssText = arr
  }
})

使用自定义指令

<p v-css="{color: 'orange', 'font-size': '24px'}">我是p标签</p>

1.png

相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!

推荐阅读:

Vue.js的计算属性和数据监听

Vue.js的事件绑定 - 内置事件绑定、自定义事件绑定

Vue.js的列表渲染 v-for 数组 对象 子组件

以上就是Vue.js的自定义指令 directive的详细内容,更多请关注php中文网其它相关文章!

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
上一条:Vue.js之js实现过渡 下一条:Vue.js的插件