Skip to content
Advertisement

Conditional logic in Vue.js

Is there any way to add conditional logic to a string template in Vue.js? This is what I tried however compiling fails.

JavaScript

Advertisement

Answer

You have to use v-if directive.

v-if is “real” conditional rendering because it ensures that event listeners and child components inside the conditional block are properly destroyed and re-created during toggles.

JavaScript

Read more about in this documentation.

JavaScript
JavaScript

Also, you can use a computed method. See reference here.

JavaScript
JavaScript
Advertisement