access store outside of component vuejs
Asked Answered
P

2

41

I have a file for configuring my OpenID Connect authentication

export const authMgr = new Oidc.UserManager({
  userStore: new Oidc.WebStorageStateStore(),
  authority: **appsetting.oidc**
})

I want to access my state in order to get the value of appsetting.

I did this:

import store from './store'

const appsetting = () => store.getters.appsetting

but my appsetting is always returning undefined

what I my missing?

Store:

app.js

const state = {
  appsetting: appsetting,
}

export {
  state 
}

getters.js

const appsetting = state => state.appsetting

export {
  appsetting
}

index.js

export default new Vuex.Store({
  actions,
  getters,
  modules: {
    app
  },
  strict: debug,
  plugins: [createLogger]
})

when I print the value of store.getters, it returns this:

{
  return __WEBPACK_IMPORTED_MODULE_2__store__["a" /* default */].getters;
}

No the actual store objects

Phylys answered 30/11, 2017 at 10:47 Comment(6)
Is appsetting getter is a part of any store module?Therron
Yes, it is part of the modulePhylys
Can you post your store module?Persist
How do you import in index.js?Quadragesimal
Does this answer your question? accessing vuex store in js fileDisrepair
You probably have an index.js/main.js file in you /store folder. You want to import that: import store from './store/index' then it should work.Disrepair
T
82

Try to import 'store' with curly brackets

import {store} from '../store/index'

store.getters.appSettings

Another option is to access from the vue property

import Vue from 'vue'

Vue.store.getters.appSettings
Terrence answered 30/11, 2017 at 14:24 Comment(6)
An example of how to access a nested module's getter (and also pass a parameter) is const hasTextAutomationEnabled = store.getters['account/hasFeature']('text-automation'); (You can normally use the dot notation, but since the module is referenced with a forward-slash "/", it's easier to get the property via the array syntax with square brackets. If there is no parameter for your getter, don't put the "(param)" at the end.)Kwangchow
Anyone can explain why we need to use curly brackets?Skiing
The curly brackets around store are used because it's not a default export. If it's a default export then you don't use the curly brackets. In this example OP used export { store } instead of export default storeCorkwood
The first one works great, but the Vue.store.getters.appSettings one doesn't seem to work for me, weird...Trivium
both doesnt work on my case.. I have a modules in my store,,, but accessing them gives error,,, I tried both options above but doesnt work, im using typescriptVladimar
Isn't this going to make it not reactive? How do you use store.getters outside of export default {} and at the same time make sure that it is reactive?Blitzkrieg
F
1

As for 2023 accesing store with

import {store} from '../store/index'
store.getters.appSettings

wasnt working for me but after removing curly brackets like so:

import store from '../store/index'
store.getters.appSettings

It started working as intended

Felty answered 9/2, 2023 at 17:30 Comment(1)
I think this works only because your store is the default export. Otherwise, the curly brackets are needed as stated by Jack Barry in the accepted answer.Roughage

© 2022 - 2024 — McMap. All rights reserved.