2
I’m starting in Vuejs and have no idea how I can recover an element (if it were in pure JS would only store in a const and dp call by id), I’m trying to make a game Monty Hall, and what I want to do is that when the person click on a door the previous q was selected lose the selection automatically... ports are generated in a for loop on the main component
<div class="doors">
<div
v-for="i in doorsAmount"
:key="i"
@click="selected(i)"
:id="'door' + i"
>
<Door :number="i" />
</div>
</div>
When the div is clicked it calls the method selected(i)
, this method stores the number of the new selected port and should also change a data of the port component to q the new one was selected and the previous one missed the selection...
The port component has the following data
data() {
return {
open: false,
selected: false,
};
},
When that data selected
is changed to true is q the port is checked as selected by a class. So how could I change this component of the current port to true and the previous one to false, I don’t know how to pass/change this data.... I tried to put it in the port component props but as I could not recover the tb element did not work...
How can I pass this data and how to recover an element (previous and current port) to make this passage?
You can do it some other way?
Hi @Isaac-Bruno, the problem is that the components are in different files... then in that case in my main file I would have to change the data and the class that belong to another file, the port component file...
– M.F
If I throw the attr and the class to the main one, it will work in this case... but what if I can’t do it? How can I manipulate dds and call Arq methods from another component? I tried to put the method
setSelected(idx)
in the port Arq and use it in the main, appears on the console q the method has not been declared... Da para fazer esta manipulação em arqs diferentes? And if not, do you have any idea how to get around this situation? Thank you very much ;)– M.F
In cases where you will use components just pass the attributes (class, Selected) via bind to the child components, from the main component. See this article here: https://medium.com/@kevinbreaker/comunica%C3%A7%C3%A3o-entre-componentes-com-vuejs-8fbb1ff075b3
– Isaac Bruno
It worked. Now I’ve got Vue’s logic. Thanks
– M.F