A
A
Anastasia2021-11-24 18:20:37
Vue.js
Anastasia, 2021-11-24 18:20:37

How to interact with the state of one component in another?

Hello everyone
I've been working with VueJs for a while now, but recently I got a project that I would like to understand.

There are two components:
Header.vue
Menu.vue The Menu.vue

619e580906a97706766471.jpeg

component has a button "close right menu" , when clicked, the right menu disappears:
<button @click="menuShow = false">close</button>

data() {
    return {
      menuShow: true,
    };
  },


How can I make a button in another component, that is, in Header.vue , with the ability to open and close this menu?
I understand that I somehow need to transfer data about the state of the menu from one component to another?

I will be very grateful for the answer

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Aetae, 2021-11-24
@Aetae

For the overall state of the entire application: vuex .
To link children who cannot live without a particular parent to that parent: provide \ inject .
For a tight bunch of single components: a separate Vue.observable \ Vue.reactive object imported in the required components.
For explicit interaction of nearby components, the usual vue-flow: up events through $emit and @on, down props.

A
Alex_mos, 2021-11-24
@Alex_mos

here you can see how to interact between components

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question