我有一个对所有对象都为v的元素,并且我有css类top-class {}…我想在渲染对象中动态添加top-class到object [0],并且它会动态变化,因此必须删除旧的顶部。
有什么帮助吗?
这是代码,在元素中您可以看到类
void listProducts()
{
//list all the available products.
cout << "Available products:\n";
for(int i=0; i<numProducts; i++)
cout << products[i]->getCode() << ": " << products[i]->getName() << " @ "
<< products[i]->getPrice() << "/pound.\n";
}
答案 0 :(得分:1)
我假设您确实需要一个计算的,并且它将返回各种类名。 (很可能您没有。)
我们将其称为topClass
,不用破折号就可以了。
目前尚不清楚objects
是对象数组还是对象的对象哈希,因此我将给出两个答案。如果objects
是:
数组
<article v-for="(item, index) in objects" :class="{ [topClass]: index === 0 }">
对象(使用第三个v-for
自变量index
)
<article v-for="(item, key, index) in objects" :class="{ [topClass]: index === 0 }">
[ ]
绑定中的class
括号是es2015 computed properties,它允许动态引用密钥。
这里是demo。
另一方面,如果类名不是动态的,则根本不需要计算。您可以这样做:
数组
<article v-for="(item, index) in objects" :class="{ 'top-class': index === 0 }">
对象(使用第三个v-for
自变量index
)
<article v-for="(item, key, index) in objects" :class="{ 'top-class': index === 0 }">