[英]@click event not changing data property in Vue.js
I have a v-for
loop in Vue.js.我在 Vue.js 中有一个
v-for
循环。 It has to iterate through a list to create buttons.它必须遍历一个列表来创建按钮。 I added a click event on them, so
upUrl
changes and takes their button index for its value:我在它们上添加了一个点击事件,所以
upUrl
发生了变化,并将它们的按钮索引作为其值:
<div class="mt-3" v-for="(buttonPic, index) in buttonPics"
:key="buttonPic"
>
<button class="btn btn-sm btn-success"
@click="upUrl= index">
{{ buttonPic }} </button>
</div>
I have upUrl:null
in data()
but checking in the Vue Google Chrome Console, I can see that upUrl
is never updated, and always equal null
.我在
data()
中有upUrl:null
但检查 Vue Google Chrome 控制台,我可以看到upUrl
从未更新,并且始终等于null
。
I've tried using @click="upUrl= this.index">
but I get an error: ' index' is defined but never used
.我试过使用
@click="upUrl= this.index">
但我得到一个错误:' index' is defined but never used
。
It should work, check the following example:它应该可以工作,请检查以下示例:
Vue.config.devtools = false; Vue.config.productionTip = false; new Vue({ el: '#app', data() { return { upUrl: null, buttonPics: ["pic1", "pic2", "pic3"] } }, watch: { upUrl(newVal, oldVal) { console.log(newVal, oldVal) } } })
<link type="text/css" rel="stylesheet" href="//unpkg.com/bootstrap/dist/css/bootstrap.min.css" /> <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.16/vue.js"></script> <div id="app" class="container"> <div class="mt-3" v-for="(buttonPic, index) in buttonPics":key="buttonPic"> <button class="btn btn-sm btn-success" @click="upUrl= index"> {{ buttonPic }} </button> </div> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.