3

how do I redirect a user to a 404 Not Found page if an ID does not exist?

I have a dynamic route like so:

const routes = [
  {
    path: '/',
    component: Home
  },
  {
    path: '/content',
    component: DataTable
  },
  {
    path: '/content/:NAS_ID',  <----what if there's an NAS_ID that does not exist? 
    name: 'datadetail',
    component: DataDetail

  },
  {
    path: '/404',
    component: NotFound
  },
  {
    path: '*',
    redirect: '/404'
  }
];

How would I write a navigation guard for the invalid NAS_ID? Thank you.

redshift
  • 4,815
  • 13
  • 75
  • 138
  • I think this is answered here [Here](https://stackoverflow.com/questions/45619407/how-to-create-a-404-component-in-vuejs-using-vue-router) – Paul Hebert Dec 13 '18 at 02:50
  • Don't think you can do this in your router file. You can certainly check the id in your `DataDetail` component, and manually redirect to `/404` if the `id` is not found. – Psidom Dec 13 '18 at 03:14
  • Would a `beforeEnter` nav guard be doable? Not sure how I'd write it though. how would I check if the ID is invalid? Newbie here...sorta makes sense, but doesn't stick yet. – redshift Dec 13 '18 at 03:19

0 Answers0