How to access async store data in vue-router for usage in beforeEnter hook?










9














How is it possible to access store data in beforeEnter which is retrieved asynchronously via the store action?



import store from './vuex/store';

store.dispatch('initApp'); // in here, async data will be fetched and assigned to the store's state

// following is an excerpt of the routes object:

path: '/example',
component: Example,
beforeEnter: (to, from, next) =>

if (store.state.asyncData)
// the above state is not available here, since it
// it is resolved asynchronously in the store action





This is especially important on the first page load or after a page reload, when the init data is being fetched and the router needs to wait for that data to either allow the user to access that page or not.



Is it possible for the router to "wait" for the data to be fetched?
Or what's the best way to handle navigation guard in combination with async vuex store data?



(oh and pre-populating "asyncData" can't be the solution, since the beforeEnter hook needs to make its decision on real data from the database, not default data)










share|improve this question























  • Where are you calling vuex actions to load the state?
    – Saurabh
    Mar 3 '17 at 15:11










  • via store.dispatch('initApp');
    – santacruz
    Mar 3 '17 at 15:26















9














How is it possible to access store data in beforeEnter which is retrieved asynchronously via the store action?



import store from './vuex/store';

store.dispatch('initApp'); // in here, async data will be fetched and assigned to the store's state

// following is an excerpt of the routes object:

path: '/example',
component: Example,
beforeEnter: (to, from, next) =>

if (store.state.asyncData)
// the above state is not available here, since it
// it is resolved asynchronously in the store action





This is especially important on the first page load or after a page reload, when the init data is being fetched and the router needs to wait for that data to either allow the user to access that page or not.



Is it possible for the router to "wait" for the data to be fetched?
Or what's the best way to handle navigation guard in combination with async vuex store data?



(oh and pre-populating "asyncData" can't be the solution, since the beforeEnter hook needs to make its decision on real data from the database, not default data)










share|improve this question























  • Where are you calling vuex actions to load the state?
    – Saurabh
    Mar 3 '17 at 15:11










  • via store.dispatch('initApp');
    – santacruz
    Mar 3 '17 at 15:26













9












9








9


3





How is it possible to access store data in beforeEnter which is retrieved asynchronously via the store action?



import store from './vuex/store';

store.dispatch('initApp'); // in here, async data will be fetched and assigned to the store's state

// following is an excerpt of the routes object:

path: '/example',
component: Example,
beforeEnter: (to, from, next) =>

if (store.state.asyncData)
// the above state is not available here, since it
// it is resolved asynchronously in the store action





This is especially important on the first page load or after a page reload, when the init data is being fetched and the router needs to wait for that data to either allow the user to access that page or not.



Is it possible for the router to "wait" for the data to be fetched?
Or what's the best way to handle navigation guard in combination with async vuex store data?



(oh and pre-populating "asyncData" can't be the solution, since the beforeEnter hook needs to make its decision on real data from the database, not default data)










share|improve this question















How is it possible to access store data in beforeEnter which is retrieved asynchronously via the store action?



import store from './vuex/store';

store.dispatch('initApp'); // in here, async data will be fetched and assigned to the store's state

// following is an excerpt of the routes object:

path: '/example',
component: Example,
beforeEnter: (to, from, next) =>

if (store.state.asyncData)
// the above state is not available here, since it
// it is resolved asynchronously in the store action





This is especially important on the first page load or after a page reload, when the init data is being fetched and the router needs to wait for that data to either allow the user to access that page or not.



Is it possible for the router to "wait" for the data to be fetched?
Or what's the best way to handle navigation guard in combination with async vuex store data?



(oh and pre-populating "asyncData" can't be the solution, since the beforeEnter hook needs to make its decision on real data from the database, not default data)







javascript vue.js vuex vue-router






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 at 10:11









Ulysse BN

2,27511231




2,27511231










asked Mar 3 '17 at 13:02









santacruz

5772717




5772717











  • Where are you calling vuex actions to load the state?
    – Saurabh
    Mar 3 '17 at 15:11










  • via store.dispatch('initApp');
    – santacruz
    Mar 3 '17 at 15:26
















  • Where are you calling vuex actions to load the state?
    – Saurabh
    Mar 3 '17 at 15:11










  • via store.dispatch('initApp');
    – santacruz
    Mar 3 '17 at 15:26















Where are you calling vuex actions to load the state?
– Saurabh
Mar 3 '17 at 15:11




Where are you calling vuex actions to load the state?
– Saurabh
Mar 3 '17 at 15:11












via store.dispatch('initApp');
– santacruz
Mar 3 '17 at 15:26




via store.dispatch('initApp');
– santacruz
Mar 3 '17 at 15:26












3 Answers
3






active

oldest

votes


















8














You can do it by returning a promise from vuex action, as it is explained here and call the dispatch from within the beforeEnter itself.



Code should look like following:



import store from './vuex/store';


// following is an excerpt of the routes object:

path: '/example',
component: Example,
beforeEnter: (to, from, next) =>

store.dispatch('initApp').then(response =>
// the above state is not available here, since it
// it is resolved asynchronously in the store action
, error =>
// handle error here
)







share|improve this answer






















  • true, that would work. but since the user could technically enter the app via any route / url i would need to include the store.dispatch + promise in every single beforeEnter hook that needs the init data. i guess this could be a use case for the beforeEach hook, but then again the initApp action does not have to be triggered on every single route request, only the ones that need authorization
    – santacruz
    Mar 3 '17 at 15:47










  • @santacruz I did not got if you have any query from your comment. One more thing, if you have a common component in all the routes which need authorization, you can also use beforeRouteEnter hook in that component which will take care of all the routes.
    – Saurabh
    Mar 3 '17 at 15:51







  • 1




    what if you are creating a store everytime for SSR? ssr.vuejs.org/en/data.html
    – Amrit Kahlon
    Oct 17 '17 at 23:20


















4














Do you really need to fetch the data asynchronously from the server before every route change, or do you simply need to persist some data from the store so that they do not "disappear" when the page is reloaded or when user uses a direct link?



If the latter is the case, you can persist the auth data (e.g. JWT token) in localStorage/cookie and then simply pull them from the storage during initialization (and commit them to store) - this should be a synchronous action.



You can also persist the whole state of the store by using vuex-persistedstate so that it doesn't disappear on reload and doesn't need to be hydrated.



If you need to fetch some data asynchronously once before first load or page reload, you can dispatch a store action and initialize the Vue instance in the then() callback - but this might depend on your implementation. Something like this (in main.js):



import Vue from 'vue';
import VueRouter from 'vue-router';
import sync from 'vuex-router-sync';

// contains VueRouter instance and route definitions
import router from 'src/router/router';
// contains Vuex.Store instance. I guess this is './vuex/store' in your example?
import store from 'src/store/store';

// sync the router with vuex store using vuex-router-sync
Vue.use(VueRouter);
sync(store, router);

// dispatch init action, doing asynchronous stuff, commiting to store
store.dispatch('initApp').then(() =>
// create the main Vue instance and mount it
new Vue(
router,
store
).$mount('#app');
);





share|improve this answer






























    4














    I've solved this issue by using store.watch() for no initial value and return the latest value when already initialized.



    Here is my sample code



    async function redirectIfNotAuth (to, from, next) 
    const user = await getUserState()
    if (user === null)
    next( name: 'auth' )
    else
    next()



    function getUserState ()
    return new Promise((resolve, reject) =>
    if (store.state.user === undefined)
    const unwatch = store.watch(
    () => store.state.user,
    (value) =>
    unwatch()
    resolve(value)

    )
    else
    resolve(store.state.user)

    )



    /* STORE */
    const store = new Vuex.Store(
    state:
    user: undefined

    )

    /* ROUTER */
    new Router(
    routes: [

    path: '',
    name: 'home',
    component: Home,
    beforeEnter: redirectIfNotAuth
    ,

    path: '/signin',
    name: 'auth',
    component: Auth,

    ]
    )





    share|improve this answer




















      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f42579601%2fhow-to-access-async-store-data-in-vue-router-for-usage-in-beforeenter-hook%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      8














      You can do it by returning a promise from vuex action, as it is explained here and call the dispatch from within the beforeEnter itself.



      Code should look like following:



      import store from './vuex/store';


      // following is an excerpt of the routes object:

      path: '/example',
      component: Example,
      beforeEnter: (to, from, next) =>

      store.dispatch('initApp').then(response =>
      // the above state is not available here, since it
      // it is resolved asynchronously in the store action
      , error =>
      // handle error here
      )







      share|improve this answer






















      • true, that would work. but since the user could technically enter the app via any route / url i would need to include the store.dispatch + promise in every single beforeEnter hook that needs the init data. i guess this could be a use case for the beforeEach hook, but then again the initApp action does not have to be triggered on every single route request, only the ones that need authorization
        – santacruz
        Mar 3 '17 at 15:47










      • @santacruz I did not got if you have any query from your comment. One more thing, if you have a common component in all the routes which need authorization, you can also use beforeRouteEnter hook in that component which will take care of all the routes.
        – Saurabh
        Mar 3 '17 at 15:51







      • 1




        what if you are creating a store everytime for SSR? ssr.vuejs.org/en/data.html
        – Amrit Kahlon
        Oct 17 '17 at 23:20















      8














      You can do it by returning a promise from vuex action, as it is explained here and call the dispatch from within the beforeEnter itself.



      Code should look like following:



      import store from './vuex/store';


      // following is an excerpt of the routes object:

      path: '/example',
      component: Example,
      beforeEnter: (to, from, next) =>

      store.dispatch('initApp').then(response =>
      // the above state is not available here, since it
      // it is resolved asynchronously in the store action
      , error =>
      // handle error here
      )







      share|improve this answer






















      • true, that would work. but since the user could technically enter the app via any route / url i would need to include the store.dispatch + promise in every single beforeEnter hook that needs the init data. i guess this could be a use case for the beforeEach hook, but then again the initApp action does not have to be triggered on every single route request, only the ones that need authorization
        – santacruz
        Mar 3 '17 at 15:47










      • @santacruz I did not got if you have any query from your comment. One more thing, if you have a common component in all the routes which need authorization, you can also use beforeRouteEnter hook in that component which will take care of all the routes.
        – Saurabh
        Mar 3 '17 at 15:51







      • 1




        what if you are creating a store everytime for SSR? ssr.vuejs.org/en/data.html
        – Amrit Kahlon
        Oct 17 '17 at 23:20













      8












      8








      8






      You can do it by returning a promise from vuex action, as it is explained here and call the dispatch from within the beforeEnter itself.



      Code should look like following:



      import store from './vuex/store';


      // following is an excerpt of the routes object:

      path: '/example',
      component: Example,
      beforeEnter: (to, from, next) =>

      store.dispatch('initApp').then(response =>
      // the above state is not available here, since it
      // it is resolved asynchronously in the store action
      , error =>
      // handle error here
      )







      share|improve this answer














      You can do it by returning a promise from vuex action, as it is explained here and call the dispatch from within the beforeEnter itself.



      Code should look like following:



      import store from './vuex/store';


      // following is an excerpt of the routes object:

      path: '/example',
      component: Example,
      beforeEnter: (to, from, next) =>

      store.dispatch('initApp').then(response =>
      // the above state is not available here, since it
      // it is resolved asynchronously in the store action
      , error =>
      // handle error here
      )








      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited May 23 '17 at 12:02









      Community

      11




      11










      answered Mar 3 '17 at 15:39









      Saurabh

      28.3k1596161




      28.3k1596161











      • true, that would work. but since the user could technically enter the app via any route / url i would need to include the store.dispatch + promise in every single beforeEnter hook that needs the init data. i guess this could be a use case for the beforeEach hook, but then again the initApp action does not have to be triggered on every single route request, only the ones that need authorization
        – santacruz
        Mar 3 '17 at 15:47










      • @santacruz I did not got if you have any query from your comment. One more thing, if you have a common component in all the routes which need authorization, you can also use beforeRouteEnter hook in that component which will take care of all the routes.
        – Saurabh
        Mar 3 '17 at 15:51







      • 1




        what if you are creating a store everytime for SSR? ssr.vuejs.org/en/data.html
        – Amrit Kahlon
        Oct 17 '17 at 23:20
















      • true, that would work. but since the user could technically enter the app via any route / url i would need to include the store.dispatch + promise in every single beforeEnter hook that needs the init data. i guess this could be a use case for the beforeEach hook, but then again the initApp action does not have to be triggered on every single route request, only the ones that need authorization
        – santacruz
        Mar 3 '17 at 15:47










      • @santacruz I did not got if you have any query from your comment. One more thing, if you have a common component in all the routes which need authorization, you can also use beforeRouteEnter hook in that component which will take care of all the routes.
        – Saurabh
        Mar 3 '17 at 15:51







      • 1




        what if you are creating a store everytime for SSR? ssr.vuejs.org/en/data.html
        – Amrit Kahlon
        Oct 17 '17 at 23:20















      true, that would work. but since the user could technically enter the app via any route / url i would need to include the store.dispatch + promise in every single beforeEnter hook that needs the init data. i guess this could be a use case for the beforeEach hook, but then again the initApp action does not have to be triggered on every single route request, only the ones that need authorization
      – santacruz
      Mar 3 '17 at 15:47




      true, that would work. but since the user could technically enter the app via any route / url i would need to include the store.dispatch + promise in every single beforeEnter hook that needs the init data. i guess this could be a use case for the beforeEach hook, but then again the initApp action does not have to be triggered on every single route request, only the ones that need authorization
      – santacruz
      Mar 3 '17 at 15:47












      @santacruz I did not got if you have any query from your comment. One more thing, if you have a common component in all the routes which need authorization, you can also use beforeRouteEnter hook in that component which will take care of all the routes.
      – Saurabh
      Mar 3 '17 at 15:51





      @santacruz I did not got if you have any query from your comment. One more thing, if you have a common component in all the routes which need authorization, you can also use beforeRouteEnter hook in that component which will take care of all the routes.
      – Saurabh
      Mar 3 '17 at 15:51





      1




      1




      what if you are creating a store everytime for SSR? ssr.vuejs.org/en/data.html
      – Amrit Kahlon
      Oct 17 '17 at 23:20




      what if you are creating a store everytime for SSR? ssr.vuejs.org/en/data.html
      – Amrit Kahlon
      Oct 17 '17 at 23:20













      4














      Do you really need to fetch the data asynchronously from the server before every route change, or do you simply need to persist some data from the store so that they do not "disappear" when the page is reloaded or when user uses a direct link?



      If the latter is the case, you can persist the auth data (e.g. JWT token) in localStorage/cookie and then simply pull them from the storage during initialization (and commit them to store) - this should be a synchronous action.



      You can also persist the whole state of the store by using vuex-persistedstate so that it doesn't disappear on reload and doesn't need to be hydrated.



      If you need to fetch some data asynchronously once before first load or page reload, you can dispatch a store action and initialize the Vue instance in the then() callback - but this might depend on your implementation. Something like this (in main.js):



      import Vue from 'vue';
      import VueRouter from 'vue-router';
      import sync from 'vuex-router-sync';

      // contains VueRouter instance and route definitions
      import router from 'src/router/router';
      // contains Vuex.Store instance. I guess this is './vuex/store' in your example?
      import store from 'src/store/store';

      // sync the router with vuex store using vuex-router-sync
      Vue.use(VueRouter);
      sync(store, router);

      // dispatch init action, doing asynchronous stuff, commiting to store
      store.dispatch('initApp').then(() =>
      // create the main Vue instance and mount it
      new Vue(
      router,
      store
      ).$mount('#app');
      );





      share|improve this answer



























        4














        Do you really need to fetch the data asynchronously from the server before every route change, or do you simply need to persist some data from the store so that they do not "disappear" when the page is reloaded or when user uses a direct link?



        If the latter is the case, you can persist the auth data (e.g. JWT token) in localStorage/cookie and then simply pull them from the storage during initialization (and commit them to store) - this should be a synchronous action.



        You can also persist the whole state of the store by using vuex-persistedstate so that it doesn't disappear on reload and doesn't need to be hydrated.



        If you need to fetch some data asynchronously once before first load or page reload, you can dispatch a store action and initialize the Vue instance in the then() callback - but this might depend on your implementation. Something like this (in main.js):



        import Vue from 'vue';
        import VueRouter from 'vue-router';
        import sync from 'vuex-router-sync';

        // contains VueRouter instance and route definitions
        import router from 'src/router/router';
        // contains Vuex.Store instance. I guess this is './vuex/store' in your example?
        import store from 'src/store/store';

        // sync the router with vuex store using vuex-router-sync
        Vue.use(VueRouter);
        sync(store, router);

        // dispatch init action, doing asynchronous stuff, commiting to store
        store.dispatch('initApp').then(() =>
        // create the main Vue instance and mount it
        new Vue(
        router,
        store
        ).$mount('#app');
        );





        share|improve this answer

























          4












          4








          4






          Do you really need to fetch the data asynchronously from the server before every route change, or do you simply need to persist some data from the store so that they do not "disappear" when the page is reloaded or when user uses a direct link?



          If the latter is the case, you can persist the auth data (e.g. JWT token) in localStorage/cookie and then simply pull them from the storage during initialization (and commit them to store) - this should be a synchronous action.



          You can also persist the whole state of the store by using vuex-persistedstate so that it doesn't disappear on reload and doesn't need to be hydrated.



          If you need to fetch some data asynchronously once before first load or page reload, you can dispatch a store action and initialize the Vue instance in the then() callback - but this might depend on your implementation. Something like this (in main.js):



          import Vue from 'vue';
          import VueRouter from 'vue-router';
          import sync from 'vuex-router-sync';

          // contains VueRouter instance and route definitions
          import router from 'src/router/router';
          // contains Vuex.Store instance. I guess this is './vuex/store' in your example?
          import store from 'src/store/store';

          // sync the router with vuex store using vuex-router-sync
          Vue.use(VueRouter);
          sync(store, router);

          // dispatch init action, doing asynchronous stuff, commiting to store
          store.dispatch('initApp').then(() =>
          // create the main Vue instance and mount it
          new Vue(
          router,
          store
          ).$mount('#app');
          );





          share|improve this answer














          Do you really need to fetch the data asynchronously from the server before every route change, or do you simply need to persist some data from the store so that they do not "disappear" when the page is reloaded or when user uses a direct link?



          If the latter is the case, you can persist the auth data (e.g. JWT token) in localStorage/cookie and then simply pull them from the storage during initialization (and commit them to store) - this should be a synchronous action.



          You can also persist the whole state of the store by using vuex-persistedstate so that it doesn't disappear on reload and doesn't need to be hydrated.



          If you need to fetch some data asynchronously once before first load or page reload, you can dispatch a store action and initialize the Vue instance in the then() callback - but this might depend on your implementation. Something like this (in main.js):



          import Vue from 'vue';
          import VueRouter from 'vue-router';
          import sync from 'vuex-router-sync';

          // contains VueRouter instance and route definitions
          import router from 'src/router/router';
          // contains Vuex.Store instance. I guess this is './vuex/store' in your example?
          import store from 'src/store/store';

          // sync the router with vuex store using vuex-router-sync
          Vue.use(VueRouter);
          sync(store, router);

          // dispatch init action, doing asynchronous stuff, commiting to store
          store.dispatch('initApp').then(() =>
          // create the main Vue instance and mount it
          new Vue(
          router,
          store
          ).$mount('#app');
          );






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Apr 11 '17 at 15:02

























          answered Apr 11 '17 at 14:29









          Arx

          24329




          24329





















              4














              I've solved this issue by using store.watch() for no initial value and return the latest value when already initialized.



              Here is my sample code



              async function redirectIfNotAuth (to, from, next) 
              const user = await getUserState()
              if (user === null)
              next( name: 'auth' )
              else
              next()



              function getUserState ()
              return new Promise((resolve, reject) =>
              if (store.state.user === undefined)
              const unwatch = store.watch(
              () => store.state.user,
              (value) =>
              unwatch()
              resolve(value)

              )
              else
              resolve(store.state.user)

              )



              /* STORE */
              const store = new Vuex.Store(
              state:
              user: undefined

              )

              /* ROUTER */
              new Router(
              routes: [

              path: '',
              name: 'home',
              component: Home,
              beforeEnter: redirectIfNotAuth
              ,

              path: '/signin',
              name: 'auth',
              component: Auth,

              ]
              )





              share|improve this answer

























                4














                I've solved this issue by using store.watch() for no initial value and return the latest value when already initialized.



                Here is my sample code



                async function redirectIfNotAuth (to, from, next) 
                const user = await getUserState()
                if (user === null)
                next( name: 'auth' )
                else
                next()



                function getUserState ()
                return new Promise((resolve, reject) =>
                if (store.state.user === undefined)
                const unwatch = store.watch(
                () => store.state.user,
                (value) =>
                unwatch()
                resolve(value)

                )
                else
                resolve(store.state.user)

                )



                /* STORE */
                const store = new Vuex.Store(
                state:
                user: undefined

                )

                /* ROUTER */
                new Router(
                routes: [

                path: '',
                name: 'home',
                component: Home,
                beforeEnter: redirectIfNotAuth
                ,

                path: '/signin',
                name: 'auth',
                component: Auth,

                ]
                )





                share|improve this answer























                  4












                  4








                  4






                  I've solved this issue by using store.watch() for no initial value and return the latest value when already initialized.



                  Here is my sample code



                  async function redirectIfNotAuth (to, from, next) 
                  const user = await getUserState()
                  if (user === null)
                  next( name: 'auth' )
                  else
                  next()



                  function getUserState ()
                  return new Promise((resolve, reject) =>
                  if (store.state.user === undefined)
                  const unwatch = store.watch(
                  () => store.state.user,
                  (value) =>
                  unwatch()
                  resolve(value)

                  )
                  else
                  resolve(store.state.user)

                  )



                  /* STORE */
                  const store = new Vuex.Store(
                  state:
                  user: undefined

                  )

                  /* ROUTER */
                  new Router(
                  routes: [

                  path: '',
                  name: 'home',
                  component: Home,
                  beforeEnter: redirectIfNotAuth
                  ,

                  path: '/signin',
                  name: 'auth',
                  component: Auth,

                  ]
                  )





                  share|improve this answer












                  I've solved this issue by using store.watch() for no initial value and return the latest value when already initialized.



                  Here is my sample code



                  async function redirectIfNotAuth (to, from, next) 
                  const user = await getUserState()
                  if (user === null)
                  next( name: 'auth' )
                  else
                  next()



                  function getUserState ()
                  return new Promise((resolve, reject) =>
                  if (store.state.user === undefined)
                  const unwatch = store.watch(
                  () => store.state.user,
                  (value) =>
                  unwatch()
                  resolve(value)

                  )
                  else
                  resolve(store.state.user)

                  )



                  /* STORE */
                  const store = new Vuex.Store(
                  state:
                  user: undefined

                  )

                  /* ROUTER */
                  new Router(
                  routes: [

                  path: '',
                  name: 'home',
                  component: Home,
                  beforeEnter: redirectIfNotAuth
                  ,

                  path: '/signin',
                  name: 'auth',
                  component: Auth,

                  ]
                  )






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Aug 9 '17 at 4:54









                  Jeng Wittawat

                  1462




                  1462



























                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f42579601%2fhow-to-access-async-store-data-in-vue-router-for-usage-in-beforeenter-hook%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      這個網誌中的熱門文章

                      Barbados

                      How to read a connectionString WITH PROVIDER in .NET Core?

                      Node.js Script on GitHub Pages or Amazon S3