vue事件在SFC HTML元素上消失

huangapple go评论79阅读模式
英文:

vue event on sfc html element dissapears

问题

我正在使用Electron Forge和Webpack + Typescript模板项目开发ElectronJs项目。

我还安装了Vue和vue-loader用于Webpack,以便我可以使用单文件组件(SFC)文件:

{
  test: /\.vue$/,
  loader: 'vue-loader'
},

一切都正常工作,但现在出现了一些奇怪的情况:当我在编辑器中添加@clickv-on:click,然后运行electron-forge start时,该事件在输出中丢失。

这是代码示例:

<button v-for="item in logFilesList?.content"
class="abc" onmouseleave="alert('hi')" v-on:click="validateAndSubmit('item')"
>{{ item }}</button>

这是我在应用程序中看到的内容:
vue事件在SFC HTML元素上消失

我已经添加了abc,所以我确定这是正确的代码片段。我还添加了onmouseleave,这个也起作用。

但是为什么v-on:click事件丢失了呢?

(我在VS Code中使用Volar)

这似乎是Webpack的输出,onClick在其中:

((0,vue__WEBPACK_IMPORTED_MODULE_0__.openBlock)(), 
(0,vue__WEBPACK_IMPORTED_MODULE_0__.createElementBlock)("button", {
  class: "abc",
  onmouseleave: "alert('hi')",
  onClick: _cache[0] || (_cache[0] = $event => 
  ($options.validateAndSubmit(item)))
}, 
(0,vue__WEBPACK_IMPORTED_MODULE_0__.toDisplayString)(item), 1 /* TEXT */))

更新

我现在明白我犯了一个错误,按钮没有响应,我假设点击事件应该在按钮元素中。然后忘记检查事件监听器选项卡。

英文:

I am working on a ElectronJs Project using Electron Forge and the Webpack + Typescript template project

I also installed Vue, and vue-loader for webpack so I could use Single File Component (SFC) file:

  {
    test: /\.vue$/,
    loader: &#39;vue-loader&#39;
  },

This is all working fine, but now something strange happens: when I add @click or v-on:click in the editor and do 'electron-forge start', this event is missing in the output.

This is the code:

    &lt;button v-for=&quot;item in logFilesList?.content&quot;
    class=&quot;abc&quot; onmouseleave=&quot;alert(&#39;hi&#39;)&quot; v-on:click=&quot;validateAndSubmit(&#39;item&#39;)&quot;
    &gt;{{ item }}&lt;/button&gt;

And this is what I see in the App:
vue事件在SFC HTML元素上消失

I have added 'abc' so I am certain this is the right piece of code. Also I added onmouseleave and this works.

But why is the v-on:click event missing?

(I am using Volar in VS Code)

This seems to be the webpack output, and the onClick is there:

((0,vue__WEBPACK_IMPORTED_MODULE_0__.openBlock)(), 
(0,vue__WEBPACK_IMPORTED_MODULE_0__.createElementBlock)(\&quot;button\&quot;, {\n        class: \&quot;abc\&quot;,\n        onmouseleave: \&quot;alert(&#39;hi&#39;)\&quot;,\n        
onClick: _cache[0] || (_cache[0] = $event =&gt; 
($options.validateAndSubmit(item)))\n      }, 
(0,vue__WEBPACK_IMPORTED_MODULE_0__.toDisplayString)(item), 1 /* TEXT 
*/))\n

Update

I see now where I went astray, the button wasn't responding and I assumed click event should be there in the button element. And forgot to check the Event-Listeners tab.

答案1

得分: 2

你似乎期望

  &lt;button @click=&quot;someFn&quot; /&gt;

...输出以下HTML标记:

  &lt;button onclick=&quot;someFn&quot; /&gt;

这是不必要的,因为Vue 直接处理事件 <sup>1</sup> ,无需额外的标记。

在Vue模板中放置的所有事件在运行时都会直接转换为实际的事件侦听器,绑定到元素在渲染后立即执行。类似于:

  &lt;button /&gt;
  &lt;script&gt;
    element.addEventListener(&#39;click&#39;, someFn)
  &lt;/script&gt;

(其中 element 是实际的 &lt;button&gt; 元素),除了在标记中没有实际的脚本标签,因为Vue有一个主动的JavaScript层,会在需要时执行所有必要的操作。

注意:
要检查元素上绑定的事件,在Google Chrome <sup>2</sup> 上右键单击元素,选择"Inspect",转到"Elements"选项卡,然后在控制台中转到"Event Listeners"子选项卡:

vue事件在SFC HTML元素上消失


<sup>1</sup> - 这对于原生事件和组件特定(也称为自定义)事件(方法处理程序,发射,观察者)都是适用的
<sup>2</sup> - 所有浏览器都具有类似的实用工具,界面可能略有不同。

英文:

You seem to expect

  &lt;button @click=&quot;someFn&quot; /&gt;

...to output the following HTML markup:

  &lt;button onclick=&quot;someFn&quot; /&gt;

That would be unnecessary, because Vue handles events <sup>1</sup> directly, without the need of additional markup.

All events placed in Vue templates are directly turned into actual event listeners at runtime, bound to the element immediately after rendering it. Something akin to:

  &lt;button /&gt;
  &lt;script&gt;
    element.addEventListener(&#39;click&#39;, someFn)
  &lt;/script&gt;

... (where element is the actual &lt;button&gt; element), except there's no actual script tag in markup, because Vue has an active JavaScript layer which does everything it needs to, when it needs to.

Note:
To inspect the bound events on an element, in Google Chrome <sup>2</sup> right-click the element, select "Inspect", go to "Elements" tab and then go to "Event Listeners" sub-tab in console:
vue事件在SFC HTML元素上消失


<sup>1</sup> - This is true for both native events and component specific (a.k.a. custom) events (method handlers, emits, watchers)
<sup>2</sup> - All browsers have similar utilities, the interface might be slightly different.

答案2

得分: 0

这是正常的、正确的行为。

v-on:click 和类似的指令是特定于Vue框架的。它们不是DOM构造,不会被渲染出来。模板代码会被提取并预编译成JavaScript渲染函数。这就是为什么这些指令在浏览器中仍然能够正常工作,但不会出现在DOM中的原因。简单来说,当它到达您的浏览器时,它已经变成了JavaScript。

英文:

This is normal, correct behavior.

v-on:click, and similar directives, are specific to the Vue framework. They are not DOM constructs, and they don't get rendered. Template code gets extracted and pre-compiled into JavaScript render functions. This is what allows the directives to still function in the browser while not appearing in the DOM. In the simplest terms, it's all JavaScript by the time it hits your browser.

huangapple
  • 本文由 发表于 2023年6月1日 18:07:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76380807.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定