Vue项目的按钮事件处理问题

zbdgwd5y  于 2023-05-18  发布在  Vue.js
关注(0)|答案(1)|浏览(194)

我是Vue的新手。我想创建一个简单的按钮点击回调,回调方法会显示按钮的名称。回调工作,但按钮名称仍然是“undefined”。
HTML代码:

  1. <button class="w-32" attr-type="submit" @click="sendClick">btnName</button>

TS代码:

  1. sendClick(event: React.MouseEvent<HTMLButtonElement>) {
  2. if (event) {
  3. const { target } = event
  4. if (target){
  5. console.log( ((target as HTMLButtonElement).value));
  6. }
  7. }
  8. }

你能告诉我我哪里做错了吗?
来源:https://vuejs.org/guide/essentials/event-handling.html#inline-handlers
https://microsoft.github.io/PowerBI-JavaScript/interfaces/node_modules_typedoc_node_modules_typescript_lib_lib_dom_d.htmlbuttonelement.html#name
https://freshman.tech/snippets/typescript/fix-value-not-exist-eventtarget/

juud5qan

juud5qan1#

答案是从主要帖子的评论中推断出来的。

  1. if (event) {
  2. const { target } = event
  3. if (target) {
  4. var el: HTMLButtonElement = (target as HTMLButtonElement)
  5. if (el && el.firstChild) {
  6. console.log(el.firstChild.textContent);
  7. }
  8. }
  9. }

相关问题