No More Posting New Topics!

If you have a question or an issue, please start a thread in our Github Discussions Forum.
This forum is closed for new threads/ topics.

Navigation

    Quasar Framework

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search

    Access store within Service class using typescript

    Help
    3
    6
    1042
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • D
      daniel last edited by

      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 beets 2 Replies Last reply Reply Quote 0
      • metalsadman
        metalsadman @daniel last edited by

        @daniel https://forum.quasar-framework.org/topic/4685/trouble-accessing-data-from-boot-file/4?_=1599922933710

        D 1 Reply Last reply Reply Quote 0
        • D
          daniel @metalsadman last edited by

          @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.

          metalsadman 1 Reply Last reply Reply Quote 0
          • metalsadman
            metalsadman @daniel last edited by metalsadman

            @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.

            D 1 Reply Last reply Reply Quote 0
            • D
              daniel @metalsadman last edited by

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

              1 Reply Last reply Reply Quote 0
              • beets
                beets @daniel last edited by

                @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

                1 Reply Last reply Reply Quote 0
                • First post
                  Last post