javascript - How to export Router instance in Vue.js 2 - Stack Overflow

I'm creating a Router instance in order to implement a routing system in my app. So, I'm doin

I'm creating a Router instance in order to implement a routing system in my app. So, I'm doing it in such a way:

import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

export default new Router({
  routes: [
    {
      path: '/',
      name: 'Home',
      ponent: Home
    },
  ]
})

But I also need to implement a .beforeEach() hook on my Router instance. What's the workaround?

I find the way like:

router.beforeEach((to, from, next) => {
    // ...
});

But I guess in my case it won't be correct? What's the proper way of doing it?

I'm creating a Router instance in order to implement a routing system in my app. So, I'm doing it in such a way:

import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

export default new Router({
  routes: [
    {
      path: '/',
      name: 'Home',
      ponent: Home
    },
  ]
})

But I also need to implement a .beforeEach() hook on my Router instance. What's the workaround?

I find the way like:

router.beforeEach((to, from, next) => {
    // ...
});

But I guess in my case it won't be correct? What's the proper way of doing it?

Share Improve this question edited Jul 6, 2021 at 19:42 Sweet Western 34 bronze badges asked Feb 13, 2018 at 5:51 Камилов ТимурКамилов Тимур 8982 gold badges14 silver badges30 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

Solved:

import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

const router = new Router({
  routes: [
    {
      path: '/',
      name: 'Home',
      ponent: Home
    },
  ]
})

Implementing .beforeEach() hook on router instance:

router.beforeEach((to, from, next) => {
  if(to.meta.requiresAuth) { 
    if(store.state.session.authenticated) { 
        next();
    }
    else {
        next('/admin/login');
    }
  }
  else {
    next();
  }
});

Exporting the instance:

export default router;

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744234350a4564402.html

相关推荐

  • javascript - How to export Router instance in Vue.js 2 - Stack Overflow

    I'm creating a Router instance in order to implement a routing system in my app. So, I'm doin

    8天前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信