How do you eventBus a bus to communicate updates to a view in a Vue component?

后端 未结 2 514
失恋的感觉
失恋的感觉 2021-01-23 03:00

Listen for custom events for the bus in component b. However, after dispatching events in component a, it accesses component b. the listening function of component b is executed

2条回答
  •  有刺的猬
    2021-01-23 03:41

    This will work only if both component A and component B are present in the page at the time you are emitting. From the code it seems that you are emitting the value from component A and then navigating to component B and expecting the value there.

    What you are doing is something like kicking a ball and then running after it and then picking it only to find that the ball has disappeared. What you need is another person already present at that location who picks up the ball.

    A solution in this case can be to set the value in localstorage, navigate to the other route and then read the value from localstorage.

    If the value you need to pass is a simple value, you can just pass it in query string and then read from $router params in component B.

提交回复
热议问题