Adding & Removing Classes in Vue with Ternary Operators
To add or remove a class in an element in Vue using a ternary operator, you can use the ':class' binding. Here's an example:
<div :class="{'existing-class': true, 'new-class': isActive}">
<!-- content -->
</div>
In this example, the 'existing-class' is a static class that is always present on the element. The 'new-class' class will be added to the element only when the 'isActive' data property is truthy.
To remove a class using a ternary operator, you can use the negation operator '!' to reverse the truthiness of a data property. For example, to remove the 'new-class' when 'isActive' is falsy, you can do:
<div :class="{'existing-class': true, 'new-class': !isActive}">
<!-- content -->
</div>
This will remove the 'new-class' from the element when 'isActive' is falsy.
原文地址: https://www.cveoy.top/t/topic/nV8C 著作权归作者所有。请勿转载和采集!