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

标签 vue.js vue-router

我尝试以这种方式将 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>

这不起作用,因为每次 View 渲染时都会执行 go('home') ,而不是在触摸/点击时执行。

最佳答案

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

试试这个:

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

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

关于vue.js - 如何将 vue-router 与 vue-touch-events 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52322730/

相关文章:

javascript - 在组件函数中使用 Vue 动态路由名称

vue.js - Vue &lt;script setup> - 如何处理多个动态组件?

javascript - 在 Vue.js 中迭代 JSON 和打印值

vue.js - 如何从 Vuex 状态使用 Vue Router?

vue.js - Vue test-utils 如何测试 router.push()

apache - Vue 使用 Apache 部署 : routes don't work in history mode

typescript - vue-router 3.5.1 : <router-link>'s tag prop is deprecated and has been removed in Vue Router 4 中的警告

twitter-bootstrap - 使用事件单击在 bootstrap-vue 的 b-table 上显示原始 HTML

javascript - 使用对象分配来清除状态

components - VueJs 子组件中的调用方法