nativescript-vue-navigator icon indicating copy to clipboard operation
nativescript-vue-navigator copied to clipboard

Access all routes from the $navigator object

Open mreall opened this issue 5 years ago • 2 comments
trafficstars

It would really be helpful to have access to all the routes from the $navigator object. Right now we can only access the current route.

This would be helpful, for example, if I want to define a default transition for each route in the meta object. Then when I call this.$navigator.navigate I can pass in the default transition for the route.

routes.js example:

export default {
   routes: {
      '/page1': {
         component: Page1,
         meta: {
            transition: 'fade',
         }
      },
      '/page2': {
         component: Page2,
         meta: {
            transition: 'slideBottom',
         }
      },
}

Then in a component used for routing:

  methods: {
    /***
     * Called when a user taps a navigation button.
     * @param string to Page ID from the routes.js file.
     ***/
    route(to) {
      let transition = 'fade';
      const route = this.$navigator.getRoute(to);    // getRoute is a possible new method to access the routes.
      if (route && 'transition' in route.meta) {
        transition = route.meta.transition;
      }
      this.$navigator.navigate(to, { frame: "mainContent", transition });
    },
  }

mreall avatar Jun 24 '20 15:06 mreall

Why not import router.js where you want to access.. ? Don’t know..

gravataLonga avatar Jun 24 '20 17:06 gravataLonga

That's certainly an option. As a workaround I also assigned it to Vue, i.e. Vue.prototype.$routes = routes. But a logical place for it is with the Navigator.

mreall avatar Jun 24 '20 20:06 mreall