nuxt-modules / ionic

Batteries-included, zero-config Ionic integration for Nuxt
https://ionic.nuxtjs.org
MIT License
369 stars 38 forks source link

fix: Empty `route.params` object on navigation #121

Open quezadaesteban opened 1 year ago

quezadaesteban commented 1 year ago

๐Ÿ› The bug

When navigating to a new page, the route.params object from useRoute is empty.

๐Ÿ› ๏ธ To reproduce

https://stackblitz.com/edit/github-hz29vt?file=pages/tabs/tab1.vue

๐ŸŒˆ Expected behaviour

It should be an object with the current route parameters, query, etc.

โ„น๏ธ Additional context

quezadaesteban commented 1 year ago

This is still broken on @nuxtjs/ionic v.0.8.1. w. Nuxt 3.1.0.

MikeyBeLike commented 1 year ago

I had this issue, how I fixed it was by importing it from vue router.

import { useRoute } from 'vue-router';

and using this one rather than the auto imported one

Tested on "@nuxtjs/ionic": "^0.8.0", && "nuxt": "3.0.0", Still an issue that needs to be observed

chrisspiegl commented 1 year ago

I ran into the same. The workaround provided by @MikeyBeLike seems to work.

I tried looking if there is a way to globally (in the nuxt.config.ts) import the vue-router but I have not found a way yet.

Maybe there is oneโ€ฆ please let me/us know if you know a way.

Thanks

quezadaesteban commented 1 year ago

The solution given by @MikeyBeLike worked! In my case, I have something like this in the composable (so it is global). I only had to add the import in the composable, and everything worked again as expected.

import { useRoute } from 'vue-router'

// ...

export const useNavigation = () => {
  const router = useIonRouter();
  const route = useRoute();

  return {
    route,

    push: (name: RouteName, options?: RouteOptions) => {
      router.push({ name, params: options?.params });
    },

    replace: (name: RouteName) => router.replace({ name }),
  };
};

So in my components, I can do:

const navigation  = useNavigation()
// and then
navigation.route // same as const route = useRoute()
navigation.push('home') // route name is hinted by RouteName type.
navigation.replace('home') // same as `push`

Regardless, it is still an issue that needs to be addressed.

chrisspiegl commented 1 year ago

That's "a way" but does not solve the issue of having to use something non standard.