nativescript-vue / nativescript-vue-navigator

A simple router for NativeScript-Vue, built on top of $navigateTo to simplify routing from within components
MIT License
98 stars 10 forks source link

Access all routes from the $navigator object #44

Open mreall opened 4 years ago

mreall commented 4 years ago

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 });
    },
  }
gravataLonga commented 4 years ago

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

mreall commented 4 years ago

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.