Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork5
A simple Router for NativeScript-Vue, built on top of $navigateTo so to simplify routing between the pages
License
MattCCC/nativescript-vue-router-extended
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
nativescript-vue-router-extended
NativeScript Vue Router brings easier routing handling to mobile apps, with an API compatibility with web version of Vue Router.
Please file an issue or make a PR if you spot any problems or you have any further requests regarding the functionality.
- Features
- Prerequisites / Requirements
- Installation
- Usage & Examples
- New hooks for pages
- TypeScript
- API Differences to Web
- Troubleshooting
- Same hooks and guards for mobile and web
- Additional action dispatcher to dispatch actions to store automatically when changing routes
- Vue-Router 4 API compatibility
- NativeScript-Vue compatible
- TypeScript Support out of the box
Nativescript 7.1+ is required for the plugin to run properly. It might be working on previous NS6 although the plugin is no longer officially supported for NativeScript 6.
nspluginaddnativescript-vue-router-extendedornpminstallnativescript-vue-router-extendedoryarnaddnativescript-vue-router-extended
To use this plugin you need to import it and initialize the router usingcreateRouter()
function. Global and per component Vue-Router hooks and guards are supported.
importVuefrom"nativescript-vue";import{createRouter}from"nativescript-vue-router-extended";// Initialize Example RoutesimportmoviesPagefrom"./pages/movies.vue";constroutes=[{path:"/movies",component:moviesPage,// Optionalmeta:{isVertical:true,// Example actions to dispatch automatically when page is visited// Remember that you need to implement actions in your Vuex storestore:{// Call action to hide navigation buttonsshowNavigationButtons:false,// Call "showMovies" action in "categories" module with payload "all""categories/showMovies":"all",// Call action without payloadshowNavigationButtons:null,},},// Optional, per route guards are supported// More info: https://next.router.vuejs.org/guide/advanced/navigation-guards.html#per-route-guardbeforeEnter:(to,from)=>{if(to.props.passUser){// Continue navigationreturntrue;}// Reject the navigationreturnfalse;},},];// Initialize Router// Vue-Router settings are in 1st argument. 2nd one is used for extra NativeScript related settingsconstrouter=createRouter({ routes},{// Optional settings below// Set first page to redirect to when there's no page to redirect back torouteBackFallbackPath:"/movies",// Do something straight before navigation or adjust NS routing settingsrouteToCallback:(to,options)=>{// For example, change page navigation transition for the vertical on iOSif(to.meta.isVertical){options.transition={name:"fade",};}},// Do something straight before navigation or adjust NS routing settingsrouteBackCallback:(_to,options)=>{// Do something...},// Set Vue Instance (Vue.prototype by default)vm:Vue.prototype,// Set a custom logger (console.log by default)logger:console.log,// Set custom frame, by default it's taken from @nativescript/core/ui/frameframe:Frame,});// Register a global guard (optional)// You can also use global router.beforeResolve guard and router.afterEach hookrouter.beforeEach((to)=>{// For example, verify per route access rulesif(!canAccessRoute(to)){returnfalse;}returntrue;});// From now on you can access this.$router, this.$routeBack and special this.$routeTo inside of the components, example:this.$routeTo("/movies",{// Clear History is a NativeScript settingclearHistory:true,// Route inside of custom Frameframe:"myFrameId",// Pass props to the pageprops:{movieId:12,},});
You can use hooks directly on particular pages. It is discouraged to use them inside of mixins or components for the time being. Example below:
<template><Page></Page></template><script>exportdefault{name:'movies',beforeRouteEnter(to,from,next){// Do something before to enter to the routenext((vm)=>{// Do something once navigation is done// Instead of `this`, use `vm`});},beforeRouteLeave(){// Do something before to leave the route// You can use `this` inside of this hook},beforeRouteUpdate(){// Do something before to leave the route// before redirecting to another route with same path// You can use `this` inside of this hook},mounted(){// Output current route object with name, path etc.console.log(this.$route);},};</script>
NS Event | Mapped as | Description |
---|---|---|
navigatingFrom | beforeRouteLeave | Before user leaves the route |
navigatingTo | beforeRouteEnter | Before user enters a route |
- | beforeRouteUpdate | Before route is changed but view remains the same. This can happen when path is exactly the same but you change e.g. passed prop to the route. Please refer to Vue-Router docs for more details. |
navigatedTo | beforeRouteEnter | To trigger it properly you need to access component instance. You can usenext(vm => ...) callback withinbeforeRouteEnter() . Please check Vue-Router docs for more details. |
navigatedFrom | - | This event is tricky to control for developers. There is no exact mapping of it in the router. For store state cleanup use build-in meta dispatcher instead. For component state you could opt for usingbeforeRouteLeave() . |
If you need a TS support and it's not detected automatically in your project for some reason, you can usetypings/shims.vue.d.ts to bring proper support in .vue files. You can specify it in yourshims.vue.d.ts
file (attention! Please ensure that path is relative to your node_modules directory):
/// <reference path="./node_modules/nativescript-vue-router-extended/typings/shims-vue.d.ts" />
Vue Router compatibility
This plugin aims for compatibility with Vue Router 3+ and Vue Router 4+. Both should be easily supported. Please refer toVue Router Docs for more information.
DOM & related hooks
There are some limitations like lack of DOM accessibility and related hooks and guards.
RouterLink Component
There's a lack of component due to performance reasons.
Passing props to pages
All props are passed automatically to components. Therefore you don't need to useprops: true
in your routes list.
Meta Dispatcher
An additional option that allows you to dispatch actions to Vuex store directly from routes list usingmeta.store
object. To utilize it you may need to defineVue.prototype.$store = store;
after to register your Vue store.
Apache License Version 2.0, January 2004
Please file an issue. You can use the template but it is not required. Just simply write what is your issue so we can try to reproduce and fix it.
About
A simple Router for NativeScript-Vue, built on top of $navigateTo so to simplify routing between the pages
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Sponsor this project
Uh oh!
There was an error while loading.Please reload this page.
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors3
Uh oh!
There was an error while loading.Please reload this page.