什么是直接调用方法和在函数内呼叫的vue @click的差异
我正在学习nuxt,导师的功能可以添加一个名为updateTodo
的项目,并将其连接到按钮,为follwoing
脚本
<script setup>
const updateTodo = async (id) => {
if (!input) return;
await $fetch(`/api/todo/${id}`, { method: 'PUT' });
};
</script>
template
<template>
<div class="container">
<input type="text" placeholder="Add a new todo..." v-model="input" />
<NButton @click="() => updateTodo(todo.id)">Add</NButton>
</div>
</template>
i不知道他为什么不直接称其为(例如@click =“ updateTodo(todo.id)
)。我尝试这样做。只是偏好?
I'm learning nuxt and the tutor has a function to add an item called updateTodo
and attached it to a button as the follwoing
script
<script setup>
const updateTodo = async (id) => {
if (!input) return;
await $fetch(`/api/todo/${id}`, { method: 'PUT' });
};
</script>
template
<template>
<div class="container">
<input type="text" placeholder="Add a new todo..." v-model="input" />
<NButton @click="() => updateTodo(todo.id)">Add</NButton>
</div>
</template>
I dont know why he didn't call it directly (eg. @click="updateTodo(todo.id)
). I tried to do it and it worked. Is there a reason i dont know or it's just a preference?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
两者都被允许。
https://vuejs.s.s.s.s.s.s.s.org/guide/guide/esentials/event-esentials/event------------- antling.html#听力到事件
但是,请注意,这种情况不一定总是如此。在某些库/框架中,类似
onclick =“ yourfunction()”
可能会立即调用该方法,并将返回的值用作事件侦听器。除非您要返回功能,否则这通常不是您想要的。Both are allowed.
https://vuejs.org/guide/essentials/event-handling.html#listening-to-events
However, note that this isn't necessarily always the case. In some libraries/frameworks, something like
onclick="yourFunction()"
might call the method immediately, and use the returned value as an event listener. This is usually not what you want, unless you're returning a function.a。 每次都有任何功能
b。,但是如果输入值大于100或其他任何内容,则要通过功能,那么您需要通过功能作为函数,以便将事件如类似
换句话说,如果在该特定事件的函数中没有参数,那么您就不需要传递该函数。否则您需要通过功能。
a. you call anyFunction in each time, then
b. but you want to pass function if enter value is greater than 100 or anything else then you need to pass as function so you get event as params like
In other words, if there is no argument in the function for the particular event then you don't need to pass the function. else you need to pass the function.