VueJS conditionally add an attribute for an element VueJS conditionally add an attribute for an element vue.js vue.js

VueJS conditionally add an attribute for an element


Try:

<input :required="test ? true : false">

Update: It has changed in Vue 3, see this answer https://stackoverflow.com/a/64598898


Simplest form:

<input :required="test">   // if true<input :required="!test">  // if false<input :required="!!test"> // test ? true : false


Conditional rendering of attributes changed in Vue 3. To omit an attribute use null or undefined.

Vue 2:

<div :attr="false">Result: <div><div :attr="null">Result: <div>

Vue 3:

<div :attr="false">Result: <div attr="false"><div :attr="null">Result: <div>