UNPKG

8.08 kBMarkdownView Raw
1# NativeScript Vue Router
2
3[npm-url]: https://npmjs.org/package/nativescript-vue-router-extended
4[npm-image]: http://img.shields.io/npm/v/nativescript-vue-router-extended.svg
5
6[![NPM version][npm-image]][npm-url] [![Blazing Fast](https://badgen.now.sh/badge/speed/blazing%20%F0%9F%94%A5/green)](https://github.com/MattCCC/nativescript-vue-router-extended) [![Code Coverage](https://badgen.now.sh/badge/coverage/0.00/blue)](https://github.com/MattCCC/nativescript-vue-router-extended) [![npm downloads](https://img.shields.io/npm/dm/nativescript-vue-router-extended.svg?style=flat-square)](http://npm-stat.com/charts.html?package=nativescript-vue-router-extended) [![install size](https://packagephobia.now.sh/badge?p=nativescript-vue-router-extended)](https://packagephobia.now.sh/result?p=nativescript-vue-router-extended)
7
8[nativescript-vue-router-extended](https://github.com/mattCCC/nativescript-vue-router-extended)
9
10NativeScript Vue Router brings easier routing handling to mobile apps, with an API compatibility with web version of Vue Router.
11
12Please file an issue or make a PR if you spot any problems or you have any further requests regarding the functionality.
13
14## Table of Contents
15
16- [Features](#features)
17- [Todo](#todo)
18- [Prerequisites / Requirements](#prerequisites-requirements)
19- [Installation](#installation)
20- [Usage & Examples](#usage-examples)
21- [New hooks for pages](#new-hooks-for-pages)
22- [API & Limitations](#api-limitations)
23
24## Features
25
26- Same hooks and guards for mobile and web
27- Additional action dispatcher to dispatch actions to store automatically when changing routes
28- Vue-Router 4 API compatibility
29- NativeScript-Vue compatible
30- TypeScript Support out of the box
31
32## Todo
33
34- Test coverage
35- More compatibility (PRs and issues are welcomed so don't hesistate to report please)
36
37## Prerequisites / Requirements
38
39Nativescript 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.
40
41## Installation
42
43```javascript
44tns plugin add nativescript-vue-router-extended
45
46or
47
48npm install nativescript-vue-router-extended
49
50or
51
52yarn add nativescript-vue-router-extended
53```
54
55[nativescript-vue-router-extended](https://www.npmjs.com/package/nativescript-vue-router-extended)
56
57## Usage & Examples
58
59To use this plugin you need to import it and initialize the router using `createRouter()` function. Global and per component Vue-Router hooks and guards are supported.
60
61```javascript
62import Vue from "nativescript-vue";
63import { createRouter } from "nativescript-vue-router-extended";
64
65// Initialize Example Routes
66import moviesPage from "./pages/movies.vue";
67
68const routes = [
69 {
70 path: "/movies",
71 component: moviesPage,
72 meta: {
73 isVertical: true,
74 store: {
75 showNavigationButtons: false,
76 },
77 },
78 },
79];
80
81// Initialize Router
82// Vue-Router settings are in 1st argument. 2nd one is used for extra NativeScript related settings
83const router = createRouter(
84 { routes },
85 {
86 // Optional settings below
87
88 // Set first page to redirect to when there's no page to redirect back to
89 routeBackFallbackPath: "/movies",
90
91 // Do something straight before navigation or adjust NS routing settings
92 routeToCallback: (to, options) => {
93 // For example, change page navigation transition for the vertical on iOS
94 if (to.meta.isVertical) {
95 options.transition = {
96 name: "fade",
97 };
98 }
99 },
100
101 // Do something straight before navigation or adjust NS routing settings
102 routeBackCallback: (_to, options) => {
103 //
104 },
105
106 // Set Vue Instance (Vue.prototype by default)
107 vm: Vue.prototype,
108
109 // Set a custom logger (console.log by default)
110 logger: console.log,
111
112 // Set custom frame, by default it's taken from @nativescript/core/ui/frame
113 frame: Frame,
114 }
115);
116
117// Register a global guard (optional)
118// You can also use global router.beforeResolve guard and router.afterEach hook
119router.beforeEach((to) => {
120 // For example, verify per route access rules
121 if (!canAccessRoute(to)) {
122 return false;
123 }
124
125 return true;
126});
127
128// From now on you can access this.$router, this.$routeBack and special this.$routeTo inside of the components, example:
129this.$routeTo("/movies", {
130 // Clear History is a NativeScript setting
131 clearHistory: true,
132 props: {
133 movieId: 12,
134 },
135});
136```
137
138## New hooks for pages
139
140You can use hooks directly on particular pages. It is discouraged to use them inside of mixins or components for the time being. Example below:
141
142```javascript
143<template>
144 <Page>
145 </Page>
146</template>
147
148<script>
149
150export default {
151 name: 'movies',
152
153 beforeRouteEnter(to, from, next) {
154 // Do something before to enter to the route
155 next((vm) => {
156 // Do something once navigation is done
157 // Instead of `this`, use `vm`
158 });
159 },
160
161 beforeRouteLeave() {
162 // Do something before to leave the route
163 // You can use `this` inside of this hook
164 },
165
166 beforeRouteUpdate() {
167 // Do something before to leave the route
168 // before redirecting to another route with same path
169 // You can use `this` inside of this hook
170 },
171};
172</script>
173```
174
175| NS Event | Mapped as | Description |
176| -------------- | ------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
177| navigatingFrom | `beforeRouteLeave` | Before user leaves the route |
178| navigatingTo | `beforeRouteEnter` | Before user enters a route |
179| - | `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. |
180| navigatedTo | `beforeRouteEnter` | To trigger it properly you need to access component instance. You can use `next(vm => ...)` callback within `beforeRouteEnter()`. Please check Vue-Router docs for more details. |
181| 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 using `beforeRouteLeave()`. You could potentially use `navigatedFrom` directly inside of the page but you |
182
183## API & Limitations
184
185This plugin aims for compatibility with Vue Router 3+ and Vue Router 4+. Both should be easily supported. Please refer to [Vue Router Docs](https://next.router.vuejs.org/guide/) for more information. There are some obvious limitations like lack of DOM accessibility and related events, or lack of <router-link /> component.
186
187## License
188
189Apache License Version 2.0, January 2004