cklmercer / vue-stash

Easily share reactive data between your Vue components.
MIT License
403 stars 29 forks source link

some way to add keys to store #28

Open fend25 opened 6 years ago

fend25 commented 6 years ago

Hi! Very nice library and I am using it for a long time. Thank you! One question: is there any way to add new key or sub key to store? Maybe something like store.$set('key.subkey', value)?

fend25 commented 6 years ago

I found a solution.

First, make the root vue component accessible (for example, via window)

window.$root = new Vue({
el: ...,
data: {store},
...
})

Then, use normal vue's $set method:

$root.$set($root.store, 'key', value)
//or, for a second level:
$root.$set($root.store.key, 'otherKey', value)

And we are able again to access the store: store.key.otherKey = 'anotherValue' then vue re-renders fine and completely itself, no any $forceUpdate needed.

@cklmercer, I could extend the readme with this approach. Are you interested?