简体   繁体   English

Onclick 与 Vue.JS

[英]Onclick with Vue.JS

I have this two buttons:我有这两个按钮:

<button onclick="filterSelection('Action')">Action</button>

and

<button onclick="filterSelection('Adventure')">Adventure</button>

Now, I want to do the same but with Vue.JS, so I created this array on my app.js现在,我想用 Vue.JS 做同样的事情,所以我在我的 app.js 上创建了这个数组

genres: [{name: 'Action'}, {name:'Adventure'}]

And in my HTML file:在我的 HTML 文件中:

<button v-for="genre of genres">{{genre.name}}</button>

That's works fine, but I don't know how to add the propertie with Vue.JS:这很好,但我不知道如何使用 Vue.JS 添加属性:

onclick="filterSelection('genre')"

Can you help me?你能帮助我吗?

You can do this by having the attribute v-on:click="filterSelection(genre)" on your button.您可以通过在按钮上设置属性v-on:click="filterSelection(genre)"来做到这一点。

Edit: Forgot quotes.编辑:忘记引号。

Since vue uses a virtual dom, you cannot use the "native html" events.由于 vue 使用的是虚拟 dom,所以不能使用“原生 html”事件。

Instead you define click events via an event handler [ docs ]相反,您可以通过事件处理程序定义点击事件 [文档]

<div id="example-1">
  <button v-on:click="counter += 1">
    Add 1
  </button>
  <p>
    The button above has been clicked {{ counter }} times.
  </p>
</div>

alternatively you can use the shorthand for v-on and use the @ symbol like @click=或者,您可以使用v-on的简写并使用@符号,如@click=

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

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