Wednesday, January 3, 2018

17. Using v-if and v-else to show and hide

A boolean value, bool, can be changed by clicking on a button.


This will either hide or show different h2 elements.


This is the App.vue file:


<template>
  <div>
    <button @click="bool=!bool">Show/Hide</button>
    <h2 v-if="bool">Secret 1</h2>
    <h2 v-else>Secret 2</h2>
  </div>
</template>

<script>
export default {
  data() {
    return {
      bool: false
    }
  }
}
</script>

<style>
button {
  color: white;
  background-color: green;
  border-radius: 10px;
  padding: 5px;
  display: block;
  margin: 0 auto;
}
h2 {
  color: blue;
  background-color: orange;
  width: 400px;
}
</style>

This is the output:


No comments:

Post a Comment