Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to access the store from within an event handler? #141

Open
korggy opened this issue Oct 22, 2020 · 1 comment
Open

How to access the store from within an event handler? #141

korggy opened this issue Oct 22, 2020 · 1 comment

Comments

@korggy
Copy link

korggy commented Oct 22, 2020

I am following the instructions at Optional: setup oidc-client event listeners to catch the userLoaded event. When this happens I want to make a call to my back-end api to retrieve other user-related data I want to add to the store. Can I access the store from within one of these event handlers, or do I need to catch the window event from elsewhere in my program and access the store from there?

@ux-engineer
Copy link

@korggy we have a separate listeners file. There we can import store instance and use it within event handler functions.

Or this kind of syntax can work within a callback function:

// eslint-disable-next-line global-require
const store = require('@/store').useStore();
// Oidc store module created with option namespaced: true
const token = store.state.oidc.access_token;

If your src/store/index.js has a function to return the store instance...perhaps in this case you could just export store constant, but this follows Vue 3 Composition API and composable style use function architecture.

export function useStore() {
  return store;
}

If someone has better ways I'm all eyes :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants