Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to access store in beforeEnter #26

Closed
Ridermansb opened this issue Oct 15, 2016 · 11 comments
Closed

How to access store in beforeEnter #26

Ridermansb opened this issue Oct 15, 2016 · 11 comments

Comments

@Ridermansb
Copy link

How access store in beforeEnter ?

// main.js
import store from './store'
import router from './router'
import { sync } from 'vuex-router-sync'

sync(store, router)
// routes.js
function requireAuth (to, from, next) {
  if (!store.getters.isAuthenticated) { /// THIS NOT WORK, HOW TO ACCESS STORE?
    window.location.href = '/auth/v1/auth0/connect'
  } else {
    next()
  }
}

export default [
  {
    path: '/', component: Timeline
  },

  {
    path: '/events',
    component: Events,
    beforeEnter: requireAuth
  }
]
@yyx990803
Copy link
Member

Just import the store.

@Ridermansb
Copy link
Author

It will create a new store...

// My store/index.js
import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const store = new Vuex.Store({
  state: {},
  modules: {    ....    }
})

export default store

It recommended import again?

@LinusBorg
Copy link
Member

It will create a new store...

No it won't.

@frankmichel
Copy link

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

This is especially important after a page reload, when some 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?

@LinusBorg
Copy link
Member

LinusBorg commented Mar 3, 2017

This is a closed ticket, and a usage/support question, and the issue tracker is reserved exclusively for bug reports and feature requests (as outlined in our Contributing Guide).

We encourage you to ask it on the forum , Stack Overflow or on gitter and are happy to help you out there.

@frankmichel
Copy link

frankmichel commented Mar 3, 2017

Okay, okay ;) Just thought it might come in handy to ask it right here since other users will be directed here from Google with the same question... but I'll ask on SO :) Sorry, Linus!

Just for completion and future reference in case someone stumbles upon this question. I asked it here on SO: http://stackoverflow.com/questions/42579601/how-to-access-async-store-data-in-vue-router-for-usage-in-beforeenter-hook

@LinusBorg
Copy link
Member

No problem, it's just rules are rules and issues have to stay manageable ;)

@thesobercoder
Copy link

How can I start some animation on the parent component from beforeRouteEnter? Right now this seems impossible.

@LinusBorg
Copy link
Member

LinusBorg commented Sep 13, 2017

Please use forum.vuejs.org for questions.

@jonathan-soifer
Copy link

This is pure gold.

@LinusBorg
Copy link
Member

Yeah but enough comedy for now. :)

@vuejs vuejs locked and limited conversation to collaborators Sep 14, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants