简体   繁体   中英

Vue.js bind id to onclick function

I have a Vue template that loops through an array and creates a table. Each item in the table also gets a button that I want to bind a click event to, passing in a token that will be used in the click function.

I got the following error when I tried passing in the token using interpolation:

onclick="getClickedResult({{result.reportToken}})": 
Interpolation inside attributes has been removed. Use v-bind or the colon shorthand instead. 
For example, instead of <div id="{{ val }}">, use <div :id="val">

I understand that I can bind the click function using v-on:click="setClickedResult()" or I can attach the token to the button using :id="val" but I am confused on how to combine these so that the token gets passed into the function correctly.

What about this:

<div v-on:click="getClickedResult(result.reportToken)">Click me!</div>

Or with the @ shorthand:

<div @click="getClickedResult(result.reportToken)">Click me!</div>

You don't need interpolation ( {{ ... }} ) inside v-on attributes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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