[英]Vue.js, toggle class on click doesn't work as expected
I'm learning Due and I'm trying something that should be easy but doesn't work and I'm sure there is something I don't understand. 我正在学习Due,并且正在尝试一些应该很容易但不起作用的事情,并且我确定有些事情我不理解。 I have to add a case to a modal...
我必须在情态中添加一个案例...
I simplify the code: I have an array of products from my parent passed the a props and to chunk them in columns I use a computed variabile. 我简化了代码:我有一个由我的父母传递来的道具组成的产品数组,并使用计算的可变性将它们分块。 In the computed variable I also add to my object array an attribute active for every object, and I need to use that attribute to add the class.
在计算的变量中,我还将对每个对象都有效的属性添加到对象数组中,并且需要使用该属性来添加类。
I cannot change the value: when I click the button the product.active
value is changed if I look the console but in my template no, it is false. 我无法更改该值:单击按钮时,如果我查看控制台,
product.active
值将更改,但在我的模板中为false。 Why 为什么
<template>
<div class="columns" v-for="products in processedProducts">
<div class="column" v-for="product in products">
<pre>{{product.active}}</pre>
<a v-on:click="activeteModal(product)">Pricy History</a>
<price-history :asin="product.asin" :active="product.active"></price-history>
</div>
</div>
</template>
<script>
import PriceHistory from '../components/PriceHistory'
export default {
props: ['results','search','maxprice','discount'],
name: 'product',
components: {
PriceHistory
},
methods: {
activeteModal: function(product){
console.log(product.active);
product.active = !product.active;
console.log(product.active);
}
},
computed: {
processedProducts() {
let products = this.results.map((obj) => {
obj.active = false;
return obj;
})
// Put Array into Chunks
let i, j, chunkedArray = [], chunk = 5;
for (i=0, j=0; i < products.length; i += chunk, j++) {
chunkedArray[j] = products.slice(i,i+chunk);
}
return chunkedArray;
}
}
}
</script>
Computed objects update lazy, set the array as a data property then it will update reactive. 计算对象会延迟更新,将数组设置为数据属性,然后它将更新反应性。 Furthermore computed-objects are by default getter-only.
此外,默认情况下,计算对象仅是吸气剂。
You better trigger a method that fills the product array when the component is mounted like this: 最好像这样安装组件时触发填充产品数组的方法:
export default {
props: ['results','search','maxprice','discount'],
name: 'product',
components: {
PriceHistory
},
data: function () {
return {
products :[]
}
},
mounted: function(){
this.processedProducts();
},
methods: {
activeteModal: function(product){
console.log(product.active);
product.active = !product.active;
console.log(product.active);
},
processedProducts() {
let products = this.results.map((obj) => {
obj.active = false;
return obj;
})
// Put Array into Chunks
let i, j, chunk = 5;
for (i=0, j=0; i < products.length; i += chunk, j++) {
this.products[j] = products.slice(i,i+chunk);
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.