Skip to content
Advertisement

Vue js – Update index value doesn’t update the second component’s view

I have this pretty simple Vue js page as below:

JavaScript

Basically it contains two Vue parts – the table and the input form. By clicking the row in the table it should change the value in the input form and but clicking the Update contact button it should update the form details.

The second part is working well – however, when I clicked the table’s row, although console.log tells me the hub.index is well updated, but the view in the input form is not.

I believe this should be a two-way binding here so I am just wondering where did I do wrong here.

Advertisement

Answer

Your hub variable is outside vue instance, so this varible is not reactive, meaning that if this variable change vue will not update the DOM. You can also define hub in the data part of vue and make it reactive.

You also need to have an event bus, as you want to communicate between your two components, You can send an event using $emit and listen to the component in other component using $on.

Here is the working fiddle for your code: https://jsfiddle.net/mimani/50ajs58L/1/

JavaScript

Here I have used event bus, but as your application grows, you can think of using a state management technique which will give you variables accessible to all the components, or you can use vuex which is popular among community. You can have a look at this and this answer for more details.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement