Dies ist eine alte Version des Dokuments!
LU05d - v-if - TBD
Learning ojectives
- I can explain what function v-if directives fulfil within the DOM.
- I can name the three v-if variants that are possible in principle.
- I can apply v-if to specific examples.
Introduction
The v-if directive in Vue.js is used to conditionally render elements in the DOM. If the condition provided in the v-if evaluates to true, the element is rendered; otherwise, it is removed from the DOM entirely. This makes v-if a powerful tool for controlling the visibility of elements based on dynamic data.
<div id="app">
<button @click="toggleMessage">Toggle Message</button>
<p v-if="isVisible">Hello, Vue.js!</p>
</div>
<script>
new Vue({
el: '#app',
data: {
isVisible: true
},
methods: {
toggleMessage() { this.isVisible = !this.isVisible;
}
}
});
</script>
In this example above, the paragraph element (<p>) is conditionally displayed based on the value of the isVisible data property. Clicking the button toggles the message's visibility.
