You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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).
8
7
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.
10
9
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
12
11
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:
14
13
15
-
## Installation
16
-
From a command prompt, run:
17
-
```shell
14
+
```Shell
18
15
$ npm install --save vue-router
19
16
```
20
17
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.
25
19
26
-
---
27
-
Require Vue, VueRouter, and let them shake hands 🤝
28
-
```js
20
+
```JavaScript
29
21
constVue=require('nativescript-vue');
30
22
constVueRouter=require('vue-router');
31
23
32
24
Vue.use(VueRouter);
33
25
```
34
26
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
+
35
31
---
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.
38
37
39
-
Also, a button to the same page with a query param `user`.
40
-
```html
38
+
```HTML
41
39
const Master = {
42
40
template: `
43
41
<Page>
@@ -52,16 +50,14 @@ const Master = {
52
50
```
53
51
54
52
---
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()"`.
58
55
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">`.
61
57
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.
63
59
64
-
```html
60
+
```HTML
65
61
const Detail = {
66
62
template: `
67
63
<Page>
@@ -78,8 +74,9 @@ const Detail = {
78
74
```
79
75
80
76
---
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
83
80
constrouter=newVueRouter({
84
81
pageRouting:true,
85
82
routes: [
@@ -91,15 +88,27 @@ const router = new VueRouter({
91
88
```
92
89
93
90
---
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
96
94
router.replace('/master');
97
95
```
98
96
99
97
---
100
-
Oh, and don't forget to tell `Vue` about your routes:
101
-
```js
98
+
Tell `Vue` about your routes.
99
+
100
+
```JavaScript
102
101
newVue({
103
102
router
104
103
}).$start();
105
104
```
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):
0 commit comments