我有一个带v-for的div,我想获取我单击的div的对象值。当我单击带有警报的div时,我试图显示数据,但是它不起作用。您能帮我解决这种情况吗?
<div id="ticketContent" v-for="ticket in tickets" @click="getTicketInformations">
<h5> Ticket : {{ ticket.ticketID }}</h5>
<p>Ticket Number : {{ticket.ticketNumber}}</p>
<p>Start Date : {{ticket.startDate}}</p>
<p>Expire Date : {{ticket.expireDate}}</p>
<p>Hours : {{ ticket.hours }} </p>
</div>
state : {
tickets :[
{"ticketID":1001,
"ticketNumber":"2783sA",
"startDate":"05/10/2018",
"expireDate":"10/21/2022",
"hours":"08:00-22:00"},
{"ticketID":1002,
"ticketNumber":"8a210j",
"startDate":"01/03/2018",
"expireDate":"01/03/2020",
"hours":"14:00-18:00"},
{"ticketID":1003,
"ticketNumber":"L90Np1",
"startDate":"10/01/2015",
"expireDate":"10/01/2020",
"hours":"10:00-20:00"},
{"ticketID":1004,
"ticketNumber":"L90Np1",
"startDate":"05/01/2015",
"expireDate":"05/01/2016",
"hours":"15:00-23:00"}],
}
<script>
import { mapGetters } from 'vuex';
export default {
computed: {
...mapGetters({
tickets : 'tickets'
}),
},
methods : {
getTicketInformations(){
alert(ticket)
}
}
}
</script>
答案 0 :(得分:0)
尝试一下:
更改vfor
<div id="ticketContent_(index)" v-for="(ticket, index) in tickets" @click="getTicketInformations(index)">
和方法:
getTicketInformations(index){
alert("clicked on ->" + document.getElementById(ticketContent_' + index + '))
}
答案 1 :(得分:0)
您可以在模型中添加add selectedTicket属性,然后在点击事件中将票证实例传递给showTicket方法。
<script src="http://unpkg.com/vue"></script>
<style>
.ticket{padding:5px; border-bottom:1px solid lightblue}
</style>
<div id="app" style="width:100vw">
Tickets:
<div class="ticket" v-for="ticket in tickets"
v-bind:key="ticketID"
@click="showTicket(ticket)">
{{ticket.ticketNumber}}
</div>
<br/>
Selected Ticket:
<div>{{selectedTicket}}</div>
</div>
<script>
new Vue({
el: "#app",
data() {
return {
selectedTicket: null,
tickets: [
{
"ticketID": 1001,
"ticketNumber": "2783sA",
"startDate": "05/10/2018",
"expireDate": "10/21/2022",
"hours": "08:00-22:00"
},
{
"ticketID": 1002,
"ticketNumber": "8a210j",
"startDate": "01/03/2018",
"expireDate": "01/03/2020",
"hours": "14:00-18:00"
}
]
}
},
methods: {
showTicket(ticket) {
this.selectedTicket = ticket;
}
}
})
</script>