frandiox / vite-ssr

Use Vite for server side rendering in Node
MIT License
830 stars 92 forks source link

Route params not exposed as props in SPA mode #29

Closed nwaughachukwuma closed 3 years ago

nwaughachukwuma commented 3 years ago

Say I define my routes as follows:

export const routes = [
  {
    path: '/',
    component: () => import('./pages/Home.vue'),
    name: Routes.Home,
  },
  {
    name: 'UserProfile',
    path: '/user/:uid',
    component: () => import('./pages/User.vue'),
    props: true,
  },
  {
    path: '/:catchAll(.*)',
    name: 'not-found',
    component: () => import('./pages/404.vue'),
  }
]

...In SPA mode, I don't get the vid param as a prop and in SSR mode, I have to patch to.meta.state as shown below to get vid param injected into the component as a prop.

router.beforeEach((to, from, next) => {
   if (to.meta.state) {
       return next()
   }
   // pass route params to the page as props
   to.meta.state = Object.assign(
    {},
    to.meta.state,
    initialState,
    initialRoute.params,
   )
  next()
})

I guess I'm not doing something right

frandiox commented 3 years ago

@nwaughachukwuma Thanks for reporting! Can you try 0.10.2 and add props: true to your routes?

nwaughachukwuma commented 3 years ago

Thank you @frandiox, it works alright now!