[Vuejs]-How to have vue v-if in App.vue โ€“ but button with toggle in component?

0๐Ÿ‘

I created sample Parent and Child components that implement the functionality you describe.

Parent.vue

<template>
  <div class="parent">
    <h3>Parent.vue</h3>
    <div class="row">
      <div class="col-md-6">
        <div v-if="showDiv">Div for child to show/hide</div>
      </div>
    </div>
    <child @toggle-parent-div="toggleDiv" />
  </div>
</template>

<script>
  import Child from './Child.vue'

  export default {
    components: {
      Child
    },
    data() {
      return {
        showDiv: true
      }
    },
    methods: {
      toggleDiv() {
        this.showDiv = !this.showDiv;
      }
    }
  }
</script>

Child.vue

<template>
  <div class="child">
    <hr>
    <h3>Child.vue</h3>
    <div class="row">
      <div class="col-md-6">
        <button class="btn btn-secondary" @click="toggleParentDiv">Click to toggle parent div</button>
      </div>
    </div>
  </div>
</template>

<script>
  export default {
    methods: {
      toggleParentDiv() {
        this.$emit('toggle-parent-div');
      }
    }
  }
</script>

Leave a comment