[Vuejs]-How to conditionally set a data-attribute in vue3?

0👍

Unlike style attribute that wants a object

<div :style="{ display: index == 0 ? "block" : undefined }" />

most of the other attributes have other value types
In your case, it’s string

<div :data-title="index == 0 ? 'main' : 'otherwise'" />

Pass undefined or null to make Vue remove the attribute completely.

<div :data-title="index == 0 ? 'main' : undefined " />

Leave a comment