javascript - Remove selected routes from production build in Vue - Stack Overflow

Is there a way to remove some routes with their linked ponents from production build of Vue app?In my a

Is there a way to remove some routes with their linked ponents from production build of Vue app?

In my app I have manager interface that only I use so there is no need to have it's logic in the production build. I want to avoid having any of manager's code actually used in the production build as I can use the manager page only during development on localhost.

Here is simple example what I have now. The managerCheck tests if user is manager to allow user to enter or to redirect him back to the homepage. This is probably enough as it is also bined with check in MongoDB but I still would love to not includes manager's ponents logic inside production build as ManagerView includes pretty powerful functions and it is better to be safe than sorry.

// router.js
// ... some imports

const userCheck = (to, from, next) => store.getters['user/user'] ?  next() : next({path: '/login'})
const managerCheck = (to, from, next) => store.getters['user/manager'] ?  next() : next({path: '/'})

export default new Router({
  mode: 'hash',
  base: process.env.BASE_URL,
  routes: [
    {
      path: '/',
      name: 'App Name',
      ponent: MainView,
    },
    {
      path: '/user',
      name: 'User',
      ponent: UserView,
      beforeEnter: userCheck
    },
    {
      path: '/manager',
      name: 'Manager',
      ponent: ManagerView,
      beforeEnter: managerCheck
    }
})

Is there a way to remove some routes with their linked ponents from production build of Vue app?

In my app I have manager interface that only I use so there is no need to have it's logic in the production build. I want to avoid having any of manager's code actually used in the production build as I can use the manager page only during development on localhost.

Here is simple example what I have now. The managerCheck tests if user is manager to allow user to enter or to redirect him back to the homepage. This is probably enough as it is also bined with check in MongoDB but I still would love to not includes manager's ponents logic inside production build as ManagerView includes pretty powerful functions and it is better to be safe than sorry.

// router.js
// ... some imports

const userCheck = (to, from, next) => store.getters['user/user'] ?  next() : next({path: '/login'})
const managerCheck = (to, from, next) => store.getters['user/manager'] ?  next() : next({path: '/'})

export default new Router({
  mode: 'hash',
  base: process.env.BASE_URL,
  routes: [
    {
      path: '/',
      name: 'App Name',
      ponent: MainView,
    },
    {
      path: '/user',
      name: 'User',
      ponent: UserView,
      beforeEnter: userCheck
    },
    {
      path: '/manager',
      name: 'Manager',
      ponent: ManagerView,
      beforeEnter: managerCheck
    }
})
Share Improve this question asked Jan 19, 2020 at 20:48 Ondřej ŠevčíkOndřej Ševčík 1,6395 gold badges21 silver badges34 bronze badges 1
  • It largely depends on how you're using Vue, but chances are process.env.NODE_ENV === 'development' ? devRoutes: prodRoutes should work. Obviously, devRoutes should be a merge of prodRoutes with whatever you don't want in prod. – tao Commented Jan 19, 2020 at 22:12
Add a ment  | 

2 Answers 2

Reset to default 7

In production, unnecessary routes can be filtered out.

Routes can be defined with productionAvailable flag.

routes: [
    {
      path: '/',
      name: 'App Name',
      ponent: MainView,
      productionAvailable: true,
    },
    {
      path: '/user',
      name: 'User',
      ponent: UserView,
      beforeEnter: userCheck,
      productionAvailable: true,
    },
    {
      path: '/manager',
      name: 'Manager',
      ponent: ManagerView,
      beforeEnter: managerCheck,
      productionAvailable: false,
    }
}]

Then filter it when exporting if the node env is set to production.

export default new Router({
  mode: 'hash',
  base: process.env.BASE_URL,
  routes: process.env.NODE_ENV === 'production' ? routes.filter((route) => route.productionAvailable) : routes,
})

I would do something like this:

// router.js
// ... some imports

const userCheck = (to, from, next) => store.getters['user/user'] ?  next() : next({path: '/login'})
const managerCheck = (to, from, next) => store.getters['user/manager'] ?  next() : next({path: '/'})

const clientRoutes = [
  {
    path: '/',
    name: 'App Name',
    ponent: MainView,
  },
  {
    path: '/user',
    name: 'User',
    ponent: UserView,
    beforeEnter: userCheck
  }
]

const managerRoutes = []

// you may have to look into process.env to set this condition correctly
if (process.env.NODE_ENV !== 'production') {
  managerRoutes.push({
    path: '/manager',
    name: 'Manager',
    ponent: ManagerView,
    beforeEnter: managerCheck
  })
}


export default new Router({
  mode: 'hash',
  base: process.env.BASE_URL,
  routes: [...clientRoutes, ...managerRoutes]
})

process.env: https://cli.vuejs/guide/mode-and-env.html#modes

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

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信