繁体   English   中英

带参数的函数不在 Javascript AddEventListener 中运行

[英]Function with parameters does not run inside Javascript AddEventListener

在 NuxtJS 中,当使用AddEventListener单击元素时,我试图运行一个带有两个参数的函数。

async handleSubmit(recipe, userRating) {some code...}

setup(recipe) {
  document.querySelectorAll('.recipe-rating > .star').forEach(function(el) {
    el.addEventListener('click', function(el) {
        const userRating = el.currentTarget.getAttribute('data-rating');
        this.handleSubmit(recipe, userRating);
    }, false);
  });
...more code...
}

所有代码都在methods: {} ,单击时出现以下错误。

Uncaught TypeError: this.handleSubmit is not a function at SVGSVGElement.eval

我该如何解决这个问题,我的代码有什么问题?

解决这个问题的方法是使用带有HTML元素的Vue.js ,而不是通过.js文件。 使用v-on:click ( se Vue docs ) 我们可以在用户单击HTML元素时运行一个函数。

<svg v-for="(el, i) in 5" :key="i" :data-rating="el" @click="handleSubmit(recipe, el)">
  <polygon />
</svg>

注意: v-on:click使用ESLint @click

暂无
暂无

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

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