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

    How can i access the store from routes.ts using typescript

    Help
    4
    6
    1868
    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.
    • L
      lesourcil last edited by

      Hi,

      i’ve created my quasar project using the CLI and im using Typescript.

      Here is my store/index.js

      import { store } from 'quasar/wrappers';
      import Vuex from 'vuex';
      
      // import example from './module-example';
      // import { ExampleStateInterface } from './module-example/state';
      import { mainModule } from './main';
      import { State } from './state';
      /*
       * If not building with SSR mode, you can
       * directly export the Store instantiation
       */
      
      export interface StateInterface {
        // Define your own store structure, using submodules if needed
        // example: ExampleStateInterface;
        // Declared as unknown to avoid linting issue. Best to strongly type as per the line above.
        example: unknown;
      }
      
      export default store(function ({ Vue }) {
        Vue.use(Vuex);
      
        const Store = new Vuex.Store<State>({
          modules: {
            main: mainModule
          },
      
          // enable strict mode (adds overhead!)
          // for dev mode only
          strict: !!process.env.DEV
        });
        return Store;
      });
      
      

      Now im my router/route.js

      import { RouteConfig } from 'vue-router';
      import store from "../store/";
      import { Notify } from 'quasar';
      
      const routes: RouteConfig[] = [
        {
          path: '/',
          component: () => import('layouts/MainLayout.vue'),
          children: [
            {
              path: '',
              component: () => import('pages/Index.vue')
            },
            { 
              path: 'aci-subnet',
              component: () => import('pages/AciSubnet.vue'),
              beforeEnter: (to, from, next) => {
                console.log(store)
                if (!store.getters.main.readIsLoggedIn) {
                  Notify.create('Please sign in to access this page')
                }
                else{
                  next()
                }
              }
            },
      }
      ...
      
      export default routes;
      

      When I try to access the store i get this error -->
      TS2339: Property ‘getters’ does not exist on type ‘StoreCallback’.

      Can anybody help me ?

      1 Reply Last reply Reply Quote 0
      • K
        kennyrafael last edited by

        I facing the same issue, not exactly on routes.ts, but I need to access store outside a component and I’m not sure of how I can do it.

        It will be amazing if someone can help!!!

        L 1 Reply Last reply Reply Quote 0
        • L
          lesourcil @kennyrafael last edited by lesourcil

          @kennyrafael

          Hey Kenny,

          i was able to fix it by export the store at initialisation

          import { store } from 'quasar/wrappers';
          import Vuex from 'vuex';
          
          // import example from './module-example';
          // import { ExampleStateInterface } from './module-example/state';
          import { mainModule } from './main';
          import { State } from './state';
          /*
           * If not building with SSR mode, you can
           * directly export the Store instantiation
           */
          
          --> let myStore
           
          export default store(function ({ Vue }) {
            Vue.use(Vuex);
          
            const Store = new Vuex.Store<State>({
              modules: {
                main: mainModule
              },
          
              // enable strict mode (adds overhead!)
              // for dev mode only
              strict: !!process.env.DEV
            });
          -->  myStore = Store
            return Store;
          });
          
          --> export  { myStore }
          

          Then import it in another vue file

          import { myStore }  from "../store";
          
          ...
          
            if (!myStore.getters.isLoggedIn) {
              ...
           }
          
          

          Enjoy

          1 Reply Last reply Reply Quote 0
          • K
            kennyrafael last edited by

            Thanks buddy!!

            1 Reply Last reply Reply Quote 0
            • T
              twa last edited by

              Hi @kennyrafael thanks for this tip.

              Is this considered a best practice or there is a better way?

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

                @twa yes it’s good, but only for non SSR apps.

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