[Vuejs]-Vue pass query parameters as component properties?

0👍

const router = new Router({
  routes: [
    {
      path: "/",
      name: "dashboard",
      meta: {
        title: "",
        roles: ["*"]
      },
      component: DashboardView,
      props: AddQueryToPropsAndRemoveQuery
    }
  ]
});

function AddQueryToPropsAndRemoveQuery(route) {

  var props = route.params;

  if (Object.entries(route.query).length > 0) {

    // combine query params into the props
    for (const [key, value] of Object.entries(route.query)) {
      props[key] = value;
    }

    router.push({ params: props, query: undefined })
  }


  return props;
}

export default router;

Leave a comment