首页 > 解决方案 > 如何运行 vue 指令功能?

问题描述

我有这个 vue 2 cli 指令

directives: {
swipe: {
  inserted: function runme(el) {
  alert(el);
    },
  },
},

如何从方法或其他指令运行 runme(obj)

标签: vue.js

解决方案


您可以将通用代码分解为可以从任何地方调用的独立方法:

function swipeInserted(el, binding, vnode) {
  alert(el)
}

export default {
  directives: {
    swipe: {
      inserted: swipeInserted,
    },
    other: {
      inserted(el, binding, vnode) {
        swipeInserted(el, binding, vnode)

        // other code specific to this directive...
      }
    }
  },
  methods: {
    runInserted() {
      const el = {/*...*/}
      const binding = {/*...*/}
      const vnode = {/*...*/}
      swipeInserted(el, binding, vnode)
    }
  }
}

推荐阅读