|
1 |
| -# nativescript-vue-router |
| 1 | +## A simple router implementation that is suitable for NativeScript-Vue. |
| 2 | + |
| 3 | +## Prerequisites / Requirements |
| 4 | + |
| 5 | +| | |
| 6 | +| --------------------------------------------- | |
| 7 | +| All your own components must have unique name | |
| 8 | +| All routes name must have unique name | |
| 9 | + |
| 10 | +## Install |
| 11 | + |
| 12 | +``` |
| 13 | +npm install nativescript-vue-router --save |
| 14 | +or |
| 15 | +yarn add nativescript-vue-router |
| 16 | +``` |
| 17 | + |
| 18 | +## Usage |
| 19 | + |
| 20 | +```js |
| 21 | +// app/router/index.js |
| 22 | + |
| 23 | +import Vue from 'nativescript-vue' |
| 24 | + |
| 25 | +import NSVueRouter from 'nativescript-vue-router' |
| 26 | + |
| 27 | +import Dashboard from './components/Dashboard' |
| 28 | +import Login from './components/Login' |
| 29 | + |
| 30 | +Vue.use(NSVueRouter) |
| 31 | + |
| 32 | +const routes = [ |
| 33 | + { |
| 34 | + name: 'dashboard.index', |
| 35 | + component: Dashboard, |
| 36 | + meta: { auth: true } |
| 37 | + }, |
| 38 | + { |
| 39 | + name: 'login.index', |
| 40 | + component: Login, |
| 41 | + meta: { guest: true } |
| 42 | + } |
| 43 | +] |
| 44 | + |
| 45 | +const router = new NSVueRouter({ |
| 46 | + ignoreSame, // <-- Optional. Will set if reject or accept navigate to same current component. |
| 47 | + routes, |
| 48 | + /* eslint-disable-next-line no-undef */ |
| 49 | + verbose: TNS_ENV !== 'production' // <-- Optional. Will output the warnings to console. |
| 50 | +}) |
| 51 | + |
| 52 | +export default router |
| 53 | +``` |
| 54 | + |
| 55 | +```js |
| 56 | +// app/app.js or app/main.js |
| 57 | + |
| 58 | +import Vue from 'nativescript-vue' |
| 59 | + |
| 60 | +import router from '@/router' |
| 61 | + |
| 62 | +new Vue({ |
| 63 | + router |
| 64 | + |
| 65 | + // ... |
| 66 | +}).$start() |
| 67 | +``` |
| 68 | + |
| 69 | +```html |
| 70 | +<!-- Your own Vue Components --> |
| 71 | +<template> |
| 72 | + <Page actionBarHidden="true"> |
| 73 | + <FlexboxLayout flexDirection="column" justifyContent="center"> |
| 74 | + <button text="Navigate direct" @tap="$router.push('dashboard.index')" /> |
| 75 | + |
| 76 | + <button text="Navigate with method" @tap="submit" /> |
| 77 | + </FlexboxLayout> |
| 78 | + </Page> |
| 79 | +</template> |
| 80 | + |
| 81 | +<script> |
| 82 | + export default { |
| 83 | + name: 'LoginIndex', |
| 84 | +
|
| 85 | + methods: { |
| 86 | + submit() { |
| 87 | + // ... |
| 88 | +
|
| 89 | + this.$router.pushClear('dashboard.index') |
| 90 | +
|
| 91 | + // ... |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | +</script> |
| 96 | +``` |
| 97 | + |
| 98 | +### Guards and other before actions |
| 99 | + |
| 100 | +```js |
| 101 | +// app/router/index.js |
| 102 | + |
| 103 | +// ... |
| 104 | + |
| 105 | +router.beforeEach((to, next) => { |
| 106 | + if (to.meta.auth && isLogged) { |
| 107 | + /* eslint-disable-next-line no-undef */ |
| 108 | + if (TNS_ENV !== 'production') { |
| 109 | + /* eslint-disable-next-line no-console */ |
| 110 | + console.error(new Error('To Login!.')) |
| 111 | + } |
| 112 | + |
| 113 | + router.pushClear('login.index') |
| 114 | + } else if (to.meta.guest && isLogged) { |
| 115 | + router.push('dashboard.index') |
| 116 | + } else { |
| 117 | + next() |
| 118 | + } |
| 119 | +}) |
| 120 | + |
| 121 | +// ... |
| 122 | +``` |
| 123 | + |
| 124 | +## API |
| 125 | + |
| 126 | +### Installing |
| 127 | + |
| 128 | +| Parameters | Type | Default | Description | |
| 129 | +| ------------ | --------- | ------- | ----------------------------------------------------------- | |
| 130 | +| `ignoreSame` | `Boolean` | `true` | Set if reject or accept navigate to same current component. | |
| 131 | +| `routes` | `Array` | `[]` | Array of objects with app's routes. | |
| 132 | +| `verbose` | `Boolean` | `false` | Show output the warnings to console. | |
| 133 | + |
| 134 | +### Navigating |
| 135 | + |
| 136 | +This package provides 3 methods for navigation, `$router.push`, `$router.pushClear` and `$router.back` |
| 137 | + |
| 138 | +| Parameters | Type | Description | |
| 139 | +| ---------- | ------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------- | |
| 140 | +| `name` | `String` | First parameter in `push` and `pushClear` methods. | |
| 141 | +| `options` | `Object` | Is an optional object, which accepts all options supported by [Manual Routing](https://nativescript-vue.org/en/docs/routing/manual-routing/#navigateto) | |
| 142 | +| `times` | `[String, Number]` | Optional parameter in `back` method that go back any times that you set. | |
| 143 | + |
| 144 | +NOTE: When `$router.pushClear` method is used the navigator stack is cleaned. |
| 145 | + |
| 146 | +## TODO |
| 147 | + |
| 148 | +- [x] Native navigation |
| 149 | +- [x] Before actions |
| 150 | +- [ ] After actions |
| 151 | +- [ ] Nested routes |
| 152 | + |
| 153 | +## Contributing |
| 154 | + |
| 155 | +Thank you for considering contributing to the NSVueRouter! Please leave your PR or [issue](https://github.com/emiliogrv/nativescript-vue-router/issues). |
| 156 | + |
| 157 | +# License |
| 158 | + |
| 159 | +[MIT](https://opensource.org/licenses/MIT) |
0 commit comments