cklmercer / vue-stash

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

Call an api from store #12

Closed ServerJunge closed 7 years ago

ServerJunge commented 7 years ago

Hi,

is it possible to make an api call from store? I would like to call an endpoint when the store is created.

My store.js

export default { user: { name: null } }

cklmercer commented 7 years ago

vue-stash doesn't include any hooks. With that in mind, you can always hook into the created life-cycle hook. The store is initialized during the beforeCreate life-cycle hook, so once created rolls around you can trust that your store is ready to go.

ServerJunge commented 7 years ago

Hmm... I want vue-stash to hold my user object wich I can get via an api call. But I don't know where I should call that api so that i can use the stored data in my different components

cklmercer commented 7 years ago

maybe this example will give you a better idea. If the example below isn't appealing, then it could be that you need something for full-featured, like vuex.

https://gist.github.com/cklmercer/096137d96986e2ecfba3d244e6f1f8ce

ServerJunge commented 7 years ago

When I try

console.log(this.$store.user.name)

I get

Uncaught TypeError: Cannot read property 'user' of undefined

I try to check for the user before routing for example

import store from './store'

console.log(store.user.name);

router.beforeEach((to, from, next) => { console.log('Router Before each') next(); })

->>> console output: null

cklmercer commented 7 years ago

So, I was finally able to reproduce this issue. Are you using the latest version of vue by chance? I think the bug might have been introduced with some of the new changes. I'm working on a proper solution now.

cklmercer commented 7 years ago

So, the issue I was able to reproduce appears to have been unrelated :(