UNPKG

8.78 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- [Prerequisites / Requirements](#prerequisites-requirements)
18- [Installation](#installation)
19- [Usage & Examples](#usage-examples)
20- [New hooks for pages](#new-hooks-for-pages)
21- [TypeScript](#typescript)
22- [API & Limitations](#api-limitations)
23- [Troubleshooting](#troubleshooting)
24
25## Features
26
27- Same hooks and guards for mobile and web
28- Additional action dispatcher to dispatch actions to store automatically when changing routes
29- Vue-Router 4 API compatibility
30- NativeScript-Vue compatible
31- TypeScript Support out of the box
32
33## Prerequisites / Requirements
34
35Nativescript 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.
36
37## Installation
38
39```javascript
40ns plugin add nativescript-vue-router-extended
41
42or
43
44npm install nativescript-vue-router-extended
45
46or
47
48yarn add nativescript-vue-router-extended
49```
50
51[![NPM](https://nodei.co/npm/nativescript-vue-router-extended.png)](https://npmjs.org/package/nativescript-vue-router-extended)
52
53## Usage & Examples
54
55To 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.
56
57```javascript
58import Vue from "nativescript-vue";
59import { createRouter } from "nativescript-vue-router-extended";
60
61// Initialize Example Routes
62import moviesPage from "./pages/movies.vue";
63
64const routes = [
65 {
66 path: "/movies",
67 component: moviesPage,
68 meta: {
69 isVertical: true,
70 store: {
71 showNavigationButtons: false,
72 },
73 },
74 },
75];
76
77// Initialize Router
78// Vue-Router settings are in 1st argument. 2nd one is used for extra NativeScript related settings
79const router = createRouter(
80 { routes },
81 {
82 // Optional settings below
83
84 // Set first page to redirect to when there's no page to redirect back to
85 routeBackFallbackPath: "/movies",
86
87 // Do something straight before navigation or adjust NS routing settings
88 routeToCallback: (to, options) => {
89 // For example, change page navigation transition for the vertical on iOS
90 if (to.meta.isVertical) {
91 options.transition = {
92 name: "fade",
93 };
94 }
95 },
96
97 // Do something straight before navigation or adjust NS routing settings
98 routeBackCallback: (_to, options) => {
99 //
100 },
101
102 // Set Vue Instance (Vue.prototype by default)
103 vm: Vue.prototype,
104
105 // Set a custom logger (console.log by default)
106 logger: console.log,
107
108 // Set custom frame, by default it's taken from @nativescript/core/ui/frame
109 frame: Frame,
110 }
111);
112
113// Register a global guard (optional)
114// You can also use global router.beforeResolve guard and router.afterEach hook
115router.beforeEach((to) => {
116 // For example, verify per route access rules
117 if (!canAccessRoute(to)) {
118 return false;
119 }
120
121 return true;
122});
123
124// From now on you can access this.$router, this.$routeBack and special this.$routeTo inside of the components, example:
125this.$routeTo("/movies", {
126 // Clear History is a NativeScript setting
127 clearHistory: true,
128 props: {
129 movieId: 12,
130 },
131});
132```
133
134## New hooks for pages
135
136You can use hooks directly on particular pages. It is discouraged to use them inside of mixins or components for the time being. Example below:
137
138```javascript
139<template>
140 <Page>
141 </Page>
142</template>
143
144<script>
145
146export default {
147 name: 'movies',
148
149 beforeRouteEnter(to, from, next) {
150 // Do something before to enter to the route
151 next((vm) => {
152 // Do something once navigation is done
153 // Instead of `this`, use `vm`
154 });
155 },
156
157 beforeRouteLeave() {
158 // Do something before to leave the route
159 // You can use `this` inside of this hook
160 },
161
162 beforeRouteUpdate() {
163 // Do something before to leave the route
164 // before redirecting to another route with same path
165 // You can use `this` inside of this hook
166 },
167
168 mounted() {
169 // Output current route object with name, path etc.
170 console.log(this.$route);
171 },
172};
173</script>
174```
175
176| NS Event | Mapped as | Description |
177| -------------- | ------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
178| navigatingFrom | `beforeRouteLeave` | Before user leaves the route |
179| navigatingTo | `beforeRouteEnter` | Before user enters a route |
180| - | `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. |
181| 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. |
182| 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()`. |
183
184## TypeScript Support
185If you need a TS support and it's not detected automatically in your project for some reason, you can use [typings/shims.vue.d.ts](https://github.com/MattCCC/nativescript-vue-router-extended/blob/master/src/typings/shims-vue.d.ts) to bring proper support in .vue files. You can specify it in your `shims.vue.d.ts` file (attention! Please ensure that path is relative to your node_modules directory):
186```
187/// <reference path="./node_modules/nativescript-vue-router-extended/typings/shims-vue.d.ts" />
188```
189
190## API & Limitations
191
192This 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.
193
194## License
195
196Apache License Version 2.0, January 2004
197
198## Troubleshooting
199
200Please 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.