S
S
Serge9991112021-03-06 14:24:15
Vue.js
Serge999111, 2021-03-06 14:24:15

How to hide a block in one component on click of a button in another vue component?

Good afternoon.

Tell. There is a button in the menu component. How to hide a div that is in another component when clicking on a button?

Both components are registered locally on the same page.

Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Gololobov, 2021-03-06
@dGololobov

parent component:

<component-one v-on:click-some-button="hideBlockkInOtherComponent"></component-one>
   <component-two ref="othercomponent"></component-two>

methods: {
    hideBlockkInOtherComponent () {
         this.$refs.othercomponent.hideBlock();
    }
}

component one:
<div><button v-on:click="sendClickForParent">click</button></div>

methods: {
    sendClickForParent () {
         this.$emit('click-some-button');
    }
}

component-two:
<div>
       <div v-if="showBlock">my block</div>
   </div>

data () {
    return {
        showBlock: true,
    };
},
methods: {
    hideBlock () {
         this.showBlock = false;
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question