gpt4 book ai didi

javascript - 使用 `v-on` 被认为是不好的做法吗?

转载 作者:搜寻专家 更新时间:2023-10-30 22:30:49 25 4
gpt4 key购买 nike

我经常读到我不应该使用 HTML 的 onclick 属性,因为它会降低代码的可读性。相反,我应该在我的 script.js 中制作事件监听器。那么您如何看待 v-on:click?它是否也被认为是不好的做法,我应该改为编写事件监听器吗?

最佳答案

You might be concerned that this whole event listening approach violates the good old rules about “separation of concerns”. Rest assured - since all Vue handler functions and expressions are strictly bound to the ViewModel that’s handling the current view, it won’t cause any maintenance difficulty. In fact, there are several benefits in using v-on:

  • It’s easier to locate the handler function implementations within your JS code by skimming the HTML template.

  • Since you don’t have to manually attach event listeners in JS, your ViewModel code can be pure logic and DOM-free. This makes it easier to test.

  • When a ViewModel is destroyed, all event listeners are automatically removed. You don’t need to worry about cleaning it up yourself.

逐字来自:https://v2.vuejs.org/v2/guide/events.html#Why-Listeners-in-HTML

关于javascript - 使用 `v-on` 被认为是不好的做法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49796564/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com