O
O
olezhenka2018-04-10 13:57:03
Vue.js
olezhenka, 2018-04-10 13:57:03

How to redirect a page before creating it with vue router?

Here is my code that does what I described:

beforeCreate() {
  const session = this.$cookie.get('session');
  if(session) this.$router.push('/search-content');
}

It redirects me from '/' to '/search-content' because there is a session cookie (user is logged in)
But the problem is that the page is redirected when all the content is already loaded, why should I load the content which I don't need

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evgeny Kulakov, 2018-04-10
@olezhenka

Perhaps it makes sense to use a router hook. Those. in a component or global transition permission hook use:

beforeRouteEnter (to, from, next) {
    const session = this.$cookie.get('session');
  if(session) next({path: '/search-content'});
  },

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question