简体   繁体   English

如何在 vue-touch-events 中使用 vue-router

[英]How to use vue-router with vue-touch-events

I tried to use vue-router with vue-touch-events this way:我尝试以这种方式将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>

This does not work as go('home') is executed every time the view renders and not on touch/tap.这不起作用,因为每次视图呈现时都会执行go('home')而不是在触摸/点击时执行。

The v-touch value must be a function; v-touch值必须是一个函数; go('home') will be called immediately and returns undefined which is not a function. go('home')将被立即调用并返回undefined这不是一个函数。

Try this instead:试试这个:

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

v-on is the only directive that accepts the syntax you used; v-on是唯一接受您使用的语法的指令; the Vue template compiler will not wrap your expression in a function for other directives which is why you must do it manually. Vue 模板编译器不会将您的表达式包装在其他指令的函数中,这就是您必须手动执行的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM