Skip to content

Commit c682954

Browse files
ikoevskarigor789
authored andcommitted
Rework vue router (#153)
* Initial rework * Removed block quote formatting * Polished content * Added links, updated intro * Addressing review feedback * Fixed link
1 parent 28d1cdd commit c682954

File tree

1 file changed

+42
-33
lines changed

1 file changed

+42
-33
lines changed

content/docs/en/routing/vue-router.md

+42-33
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,41 @@
11
---
22
title: Vue Router
3-
contributors: [eddyverbruggen, rigor789]
3+
contributors: [eddyverbruggen, rigor789, ikoevska]
44
---
55

6-
If [Manual Routing](/en/docs/routing/manual-routing) doesn't cut it for your use-case,
7-
then you'll be happy to learn [the Vue router](https://router.vuejs.org/en/) is supported.
6+
> Currently, integration with Vue Router is **experimental**. If you want to use a non-experimental approach, you can try [manual routing](/en/docs/routing/manual-routing).
87
9-
With the router, there are two types of routing that you can use. Component based routing, and page based routing.
8+
With the router, you can choose between [component-based routing](https://router.vuejs.org/api/#router-view) and page-based routing. In a mobile app, you are more likely to implement page-based routing.
109

11-
Component based routing is where you specify the `<router-view />` component in your template, and the different routes will get placed into the view, and then when navigating the views will be swapped. This is useful sometimes, but in many cases what you want is to navigate to different pages.
10+
## Install and require the plugin
1211

13-
This document documents page routing in more detail, but please note that this feature is unstable at this point, and it is recommended that you stick to manual routing if you require different pages in your application. We are hoping to change this in the near future, and it is a priority on our todo list.
12+
In the command prompt, run:
1413

15-
## Installation
16-
From a command prompt, run:
17-
```shell
14+
```Shell
1815
$ npm install --save vue-router
1916
```
2017

21-
## Usage
22-
Let's show a full example, broken down in a few pieces so we can provide some comments.
23-
Note that the Vue Router has more tricks up its sleeve, so be sure to visit
24-
[the official documentation](https://router.vuejs.org/en/).
18+
In the entry file for your app (likely, `app.js` or `main.js`), require Vue and Vue Router and let them shake hands.
2519

26-
---
27-
Require Vue, VueRouter, and let them shake hands 🤝
28-
```js
20+
```JavaScript
2921
const Vue = require('nativescript-vue');
3022
const VueRouter = require('vue-router');
3123

3224
Vue.use(VueRouter);
3325
```
3426

27+
## Usage
28+
29+
This section walks you through a complete example of page-based routing, breaking it down into key pieces and providing comments along the way.
30+
3531
---
36-
Define a `Master` page with the current router as its title (`$route.path`)
37-
and a button with a `@tap="$router.push('/detail')"` so a new page is pushed on the stack and navigated to.
32+
Define a `Master` page with the current router as its title (`$route.path`).
33+
34+
Create a button with a `@tap="$router.push('/detail')"`. When tapped, a new `Detail` page is pushed on the stack and navigated to.
35+
36+
Create a second button with a query param `user`. When tapped, it passes additional information to the `Detail` page.
3837

39-
Also, a button to the same page with a query param `user`.
40-
```html
38+
```HTML
4139
const Master = {
4240
template: `
4341
<Page>
@@ -52,16 +50,14 @@ const Master = {
5250
```
5351

5452
---
55-
Define a `Detail` page with a `NavigationButton`. On iOS this will automatically bring you back to the
56-
previous page in the stack, but for Android `tap` handler is required (which is ignored on iOS).
57-
So add `@tap="$router.back()"`.
53+
Define a `Detail` page with a `NavigationButton`. On iOS, the button automatically brings you back to the
54+
previous page in the stack. On Android, you need to add a `tap` handler (ignored on iOS) to take you back: `@tap="$router.back()"`.
5855

59-
Remember that `user` query param we passed from the second button of the `Master` page? You can use it in the `Details`
60-
page like this: `<Label :text="$route.query.user">`
56+
Use the `user` query param, defined in the `Master` page. For example, display its value as text on the `Detail` page: `<Label :text="$route.query.user">`.
6157

62-
Lastly, you can navigate back (and forth) with `$router.go(<number-of-pages>)` as demonstrated below.
58+
Create a button with `$router.go(<number-of-pages>)`. When tapped, it navigates one page back in the stack.
6359

64-
```html
60+
```HTML
6561
const Detail = {
6662
template: `
6763
<Page>
@@ -78,8 +74,9 @@ const Detail = {
7874
```
7975

8076
---
81-
Define all the pages of your application as follows:
82-
```js
77+
Create a router instance, enable page routing, and define all the pages of your app.
78+
79+
```JavaScript
8380
const router = new VueRouter({
8481
pageRouting: true,
8582
routes: [
@@ -91,15 +88,27 @@ const router = new VueRouter({
9188
```
9289

9390
---
94-
And load one of those routes when the app starts:
95-
```js
91+
Load one of the routes when the app starts.
92+
93+
```JavaScript
9694
router.replace('/master');
9795
```
9896

9997
---
100-
Oh, and don't forget to tell `Vue` about your routes:
101-
```js
98+
Tell `Vue` about your routes.
99+
100+
```JavaScript
102101
new Vue({
103102
router
104103
}).$start();
105104
```
105+
106+
## See also
107+
108+
Vue Router has more tricks up its sleeve, so be sure to visit [the official documentation](https://router.vuejs.org/en/).
109+
110+
Check out the following [NativeScript-Vue samples](https://github.com/nativescript-vue/nativescript-vue/tree/master/samples):
111+
112+
* [app-with-page-routing](nativescript-vue/samples/app/app-with-page-routing.js)
113+
* [app-with-router](nativescript-vue/samples/app/app-with-router.js)
114+
* [app-with-router-pages](nativescript-vue/samples/app/app-with-router-pages.js)

0 commit comments

Comments
 (0)