Access store within Service class using typescript



  • Hi,

    I’ve some problems accessing the store within a service class. I created this service:

    import Store from 'src/store'
    
    export class DemoService {
        public static doSomething (): void {
          Store.???
       }
    }
    

    I am not able to access any store methods. The default export in src/store/index.ts is not a Vuex.Store object. Honestly, I am not sure what it is 🙂
    It was generated by the quasar CLI and looks like this:

    export default store(function ( {Vue} ) {
       Vue.use(Vuex)
    
      const Store = new Vuex.Store<StateInterface>( {
        modules: {
            ....
        },
        ...
      })
      return Store
    }
    

    My question is, how can I use the store within my service method? Do I have to pass the store as a method parameter?

    Thanks in advance.
    Best regards,
    Daniel





  • @metalsadman I saw that post, but I was wondering, why I’ve to change that file (which was created by the quasar cli). I thought that the creators of quasar had a special intention of writing it that way.



  • @daniel best check your .quasar folder, client-entry.js file I believe, that’s where the store is created by calling that default function in store/index.js, I thinkt it’s to supply the router context inside the store among other things, but yeah check the folder to better understand it.

    It’s a boiler plate code, as long as you don’t break the intended purpose (default function and initiation), and you’ll eventually have to change it anyway, when you’re supplying a module or other store configs.



  • @metalsadman Thanks for your help. I changed it according to the post you mentioned.



  • @daniel If you ever end up using SSR, you instead need to pass the store instance to the DemoService constructor, at least that’s how I do it. Then just store it as this.store.

    The reason Quasar doesn’t export the store directly is precisely because in SSR, each store instance is separate for each request. If you aren’t using SSR then metalsadman’s answer is much easier, and you can do the same with the router instance as well.

    This is a great article by Tobias about singletons and SSR with Quasar: https://dev.to/quasar/quasar-ssr-using-cookies-with-other-libs-services-4nkl


Log in to reply