gpt4 book ai didi

javascript - vue.js 如何将单击时的类绑定(bind)到 v-for 使用模板组件创建的列表项?

转载 作者:搜寻专家 更新时间:2023-10-30 22:25:15 26 4
gpt4 key购买 nike

我正在尝试在单击模板组件中的列表项时将事件类应用于它,这应该是排他性的并从所有其他列表项中删除该类。

我试过在点击时传递一个新的数据对象并引用它,我什至使用了官方文档中的示例都无济于事,我不知道为什么它不起作用。

Vue.component('delivery-options', {
props: ['deliveries'],
template: '<li><p><strong>{{ deliveries.price }}</strong>{{ deliveries.title }}</p><p>{{ deliveries.desc }}</p></li>'
})
Vue.component('ledger', {
props: ['values'],
template: '<li><p>{{ values.title }}<span>{{ values.price }}</span></p></li>'
})
var checkout = new Vue({
el: '#checkout-app',
data: {
deliveryList: [
{ id: 0, price: '£3.99 ', title: 'Home delivery', desc: 'Lorem ipsum dolor sit amet.' },
{ id: 1, price: 'Free ', title: 'Store collection', desc: 'Lorem ipsum dolor sit amet.' },
{ id: 2, price: '£5.99 ', title: 'Precise delivery', desc: 'Lorem ipsum dolor sit amet.' }
],
valuesList: [
{ id: 0, price: '£1233.99', title: 'Order value', desc: 'Lorem ipsum dolor sit amet.' },
{ id: 1, price: '£3.99', title: 'Delivery', desc: 'Lorem ipsum dolor sit amet.' },
{ id: 2, price: '£5.99', title: 'Total', desc: 'Your total including delivery.' }
]
}
})
ul {
padding: 0;
}

ul li {
list-style-type: none;
}

.delivery-options li {
padding: 1rem;
margin: 1rem;
border-radius: 3px;
border: 1px solid grey;
}

.ledger {
padding: 1rem;
margin: 1rem;
border-radius: 3px;
border: 1px solid grey;
}
<script src="https://unpkg.com/vue"></script>
<div id="checkout-app">
<ul class="delivery-options">
<delivery-options
v-for="option in deliveryList"
v-bind:deliveries="option"
v-bind:key="option.id">
</delivery-options>
</ul>
<ul class="ledger">
<ledger
v-for="value in valuesList"
v-bind:values="value"
v-bind:key="value.id">
</ledger>
</ul>
</div>

最佳答案

你需要做这样的事情:

console.clear()

Vue.component('delivery-options', {
props: ['deliveries', "isActive"],
template: `<li @click="$emit('set-active', deliveries)" :class="{active: isActive}"><p><strong>{{ deliveries.price }}</strong>{{ deliveries.title }}</p><p>{{ deliveries.desc }}</p></li>`
})
Vue.component('ledger', {
props: ['values'],
template: '<li><p>{{ values.title }}<span>{{ values.price }}</span></p></li>'
})
var checkout = new Vue({
el: '#checkout-app',
data: {
activeDelivery: null,
deliveryList: [
{ id: 0, price: '£3.99 ', title: 'Home delivery', desc: 'Lorem ipsum dolor sit amet.' },
{ id: 1, price: 'Free ', title: 'Store collection', desc: 'Lorem ipsum dolor sit amet.' },
{ id: 2, price: '£5.99 ', title: 'Precise delivery', desc: 'Lorem ipsum dolor sit amet.' }
],
valuesList: [
{ id: 0, price: '£1233.99', title: 'Order value', desc: 'Lorem ipsum dolor sit amet.' },
{ id: 1, price: '£3.99', title: 'Delivery', desc: 'Lorem ipsum dolor sit amet.' },
{ id: 2, price: '£5.99', title: 'Total', desc: 'Your total including delivery.' }
]
},
})
ul {
padding: 0;
}

ul li {
list-style-type: none;
}

.delivery-options li {
padding: 1rem;
margin: 1rem;
border-radius: 3px;
border: 1px solid grey;
}

.ledger {
padding: 1rem;
margin: 1rem;
border-radius: 3px;
border: 1px solid grey;
}

.active {
color: red
}
<script src="https://unpkg.com/vue"></script>
<div id="checkout-app">
<ul class="delivery-options">
<delivery-options
v-for="option in deliveryList"
v-bind:deliveries="option"
v-bind:key="option.id"
v-bind:is-active="option === activeDelivery"
v-on:set-active="v => activeDelivery = v">
</delivery-options>
</ul>
<ul class="ledger">
<ledger
v-for="value in valuesList"
v-bind:values="value"
v-bind:key="value.id">
</ledger>
</ul>
</div>

基本上,使用数据属性跟踪哪个交付是事件的,然后根据它是否是事件交付在组件中设置类。为了在点击时设置主动转换,组件需要发出一个事件让父级知道点击了哪个转换。

关于javascript - vue.js 如何将单击时的类绑定(bind)到 v-for 使用模板组件创建的列表项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47638418/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com