0

I have some problems with my Vue app. I'm trying to update user information that are stored in localStorage, and I'm updating it with websockets in App.vue in mounted function, like so:

window.Echo.channel("user." + this.userData.id).listen(".user-updated", (user) => {
              localStorage.setItem('userData', JSON.stringify(user.user))
        });

And so far, so good actually, localstorage is updating realtime, but the problem is, the user must refresh the page or change route so other information that is shown in another components could be updated.

Is there any way I can update all my components at once from App.vue?

1
  • You likely should update Vuex instead and use it as single source of truth. And Vuex should sync itself with LS. Commented May 18, 2021 at 14:16

2 Answers 2

1

You can emit events using event bus, or else use Vuex. For example:

window.Echo.channel("user." + this.userData.id).listen(".user-updated", (user) => {
              localStorage.setItem('userData', JSON.stringify(user.user));

              //emit event
              EventBus.$emit('EVENT_USER_DATA_CHANGE', payLoad);
        });

Then, on other components:

 mounted () {
    EventBus.$on(‘ EVENT_USER_DATA_CHANGE’, function (payLoad) {
      ...
    });
  }
Sign up to request clarification or add additional context in comments.

Comments

0

You can use :key binding on your body or main container. (e.g. <main :key="pageTrigger">) In your listen function update pageTrigger with new Date().getTime() so it will re-render the components inside the container.

Are you using vuex too? put the pageTrigger in some store so it will be accessible from everywhere in your app.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.