如何将 vue-router 与 vue-touch-events 一起使用

How to use vue-router with vue-touch-events

我尝试以这种方式将 vue-routervue-touch-events 一起使用:

<i v-touch="go('home')" class="fas fa-bars"></i>

<script>
  export default {
    name: "Nav",
    methods: {
      go: function(state) {
        this.$router.push(state)
      }
    }
  }
</script>

这不起作用,因为 go('home') 会在每次视图呈现时执行,而不是在 touch/tap 上执行。

v-touch值必须是一个函数; go('home') 将立即被调用,而 returns undefined 不是函数。

试试这个:

<i v-touch="() => go('home')" class="fas fa-bars"></i>

v-on 是唯一接受您使用的语法的指令; Vue 模板编译器不会将您的表达式包装在其他指令的函数中,这就是为什么您必须手动执行此操作的原因。