不能在vue js中访问指令参数

Can't access directive params in vue js

本文关键字:访问 指令 参数 js vue 不能      更新时间:2023-09-26
Vue.directive('selecttwo', {
  params: ['taskId'],
  bind: function () {
    var self = this;
    $(this.el)
      .select2()
      .on('change', function () {
        self.set(this.value);
        if (!this.name.match(/[a-z]/i)) {
            console.log('task-id->'+self.params.taskId); // task-id->undefined
        }
      })
  },
});

如何获取params.taskId on onchange事件?我已经尝试过this.params.taskId,但我仍然没有定义。

也许你可以粘贴你的html代码,这将有助于找到问题。

我猜你的html属性名称是不正确的,在vue中,js使用"camelCase", html使用" kebar -case",尝试如下:

<span task-id="i am id" v-selecttwo >