nativescript-vue / nativescript-vue-navigator

A simple router for NativeScript-Vue, built on top of $navigateTo to simplify routing from within components

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Similar signature as vue-router

msaelices opened this issue · comments

For the sake of the code sharing experience, it would be great that:

  • The internal object created in the Vue instance was $router
  • The $navigator.navigate(to, options) method would be $router.push(to, options)
  • $router.replace(to) would be implemented as the current $navigator.navigate(to, {clearhistory: true})
  • $router.go(-1) would be implemented as the current $navigator.back()

If this was refactored, the basic navigation could be shared for mobile and web. And this would be an excellent starting point for a better vue-router integration with other non-web implementations.

I was able to lean on the existing $router variable by doing these hackish changes on startup as a temporary workaround. It's still better than having conditional code for web and native everywhere that needs to route somewhere... I'm sure there will be more things to add and address, but simplifies downstream code quite a bit from what I can tell.

Navigator.push = route => {
    Vue.prototype.$navigator.navigate(route);
};
Navigator.init = () => {};
Navigator.history = {
  current: "/"
};

Then when creating the Vue instance you can change to...

new Vue({
  router: Navigator,
  store,
  render: h => h("frame", [h(App)])
}).$start();