Vuex - How to persist store updates across different tabs?
Asked Answered
S

2

11

I have a Vue app using Vuex. If I have two tabs open, changes to the store in one tab do not show in the other.

How can I make sure store/data across all tabs is synced instantly?

const store = new Vuex.Store({
  state: {
    tasks: []
  },
  getters: {
    tasks: state => state.tasks
  }
}

In the template:

computed: {
  tasks: function () {
    return this.$store.getters.tasks
  }
}
Sequence answered 30/11, 2018 at 18:51 Comment(2)
could you show mutations property in your storeMammary
Hello, how did you able to solve this problem. I can save data in local store but when I switch to another tab the local storage data isn't available (as the tab url is in different domain)Birdie
L
11

This doesn't work by default. You can use vuex-shared-mutations. See here: https://github.com/xanf/vuex-shared-mutations

Laresa answered 30/11, 2018 at 19:3 Comment(0)
C
2

You should add vuex-shared-mutations module:

npm i vuex-shared-mutations

import it

import createMutationsSharer from "vuex-shared-mutations";

and then add it in your store plugins:

const store = new Vuex.Store({
 // some other code...
plugins: [createMutationsSharer({ predicate: ["mutation1", "mutation2"] })]
});

Cory answered 22/6, 2022 at 9:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.