在Vue指令中使用'this'

时间:2018-08-09 14:40:50

标签: javascript vue.js vuejs2 vue-component

在Vue指令文档中,除了元素和绑定之外,没有太多关于如何将指令与外部数据一起使用的解释。

我需要访问组件的this,但是,我无法将其作为值传递,因为我还需要传递当前的数组。如果我将其作为arg传递,则将其强制为字符串。

这是我的代码:

    directives: {
      gridView: {
        update(el, binding) {
          if(binding.value.length % 2 === 0) {
            this.gamesSwiper.swiperOption.slidesPerColumn = 2; //this is undefined as there is no access to the outside component
          }
        }
      }
    }
 <!--games is an array, which is passed down to the swiper component-->
  <swiper :options="swiperOption" ref="gamesSwiper" v-gridView="games">
    <swiper-slide  class="game-frame" v-for="game in games">
      <img class="game-icon" :src="'/static/assets/' + `${game}` + '.jpg'">
    </swiper-slide>
  </swiper>

有人知道在指令中到达组件this的好方法吗?

0 个答案:

没有答案