Skip to content

Commit 26803fa

Browse files
ikoevskarigor789
authored andcommitted
Rework manual routing (#152)
* WIP * WIP * Reworked content
1 parent 80df8f1 commit 26803fa

File tree

1 file changed

+96
-44
lines changed

1 file changed

+96
-44
lines changed

content/docs/en/routing/manual-routing.md

Lines changed: 96 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,25 @@
11
---
22
title: Manual Routing
3-
contributors: [eddyverbruggen, fartek, rigor789]
3+
contributors: [eddyverbruggen, fartek, rigor789, ikoevska]
44
---
55

6-
The easiest way to do routing in NativeScript-Vue is using the convenience functions
7-
`$navigateTo`, `$navigateBack`, and `$showModal`.
6+
The easiest way to do routing in NativeScript-Vue is by using any of the following convenience functions:
87

9-
### `$navigateTo`
10-
11-
Suppose you have components `Master` and `Detail` and want to navigate from `Master` to `Detail`,
12-
then you have two ways to call `$navigateTo`: in the view, or in a method:
8+
* [`$navigateTo`](#navigateto)
9+
* [`$navigateBack`](#navigateback)
10+
* [`$showModal`](#showmodal)
1311

14-
The `$navigateTo` accepts a second `options` parameter, which allows you to specify the transition as well as pass in a `context` object which will be used when instantiating the target component. This is useful when you want to pass props to the target component. For example:
12+
> All examples on this page discuss how to handle routing between the `Master` and `Detail` components of a mobile app.
1513
16-
```js
17-
this.$navigateTo(Detail, {
18-
transition: {},
19-
transitionIOS: {},
20-
transitionAndroid: {},
21-
22-
context: {
23-
propsData: {
24-
foo: 'bar',
25-
}
26-
}
27-
});
28-
```
14+
### `$navigateTo`
2915

30-
To read more about the options you can pass [head over to the documentation for NavigationEntry](https://docs.nativescript.org/api-reference/interfaces/_ui_frame_.navigationentry).
16+
You can call `$navigateTo` in the view or in a method.
3117

3218
#### In the view
3319

34-
Expose the `Detail` component through a `data` property in the `Master` component and invoke `$navigateTo(<propertyName>)` in the view directly.
20+
In the `Master` component, use a `data` property to expose the `Detail` component. Invoke `$navigateTo(<propertyName>)` in the view directly.
3521

36-
```vue
22+
```Vue
3723
const Vue = require('nativescript-vue');
3824
3925
const Master = {
@@ -71,9 +57,9 @@ new Vue({
7157

7258
#### In a method
7359

74-
Bind a button to a method and use `this.$navigateTo(Detail)` to navigate to the `Detail` component in that method,
60+
Bind a button to a method and use `this.$navigateTo(Detail)` to navigate to the `Detail` component.
7561

76-
```vue
62+
```Vue
7763
const Master = {
7864
template: `
7965
<Page>
@@ -103,11 +89,36 @@ const Detail = {
10389
};
10490
```
10591

92+
#### Passing props to the target component
93+
94+
`$navigateTo` accepts a second `options` parameter. You can use the parameter to:
95+
96+
* set the transition
97+
* pass a `context` object with props to be used when instantiating the target component
98+
99+
For example:
100+
101+
```JavaScript
102+
this.$navigateTo(Detail, {
103+
transition: {},
104+
transitionIOS: {},
105+
transitionAndroid: {},
106+
107+
context: {
108+
propsData: {
109+
foo: 'bar',
110+
}
111+
}
112+
});
113+
```
114+
115+
For more information about the options that you can pass, see [`NavigationEntry`](https://docs.nativescript.org/api-reference/interfaces/_ui_frame_.navigationentry).
116+
106117
### `$navigateBack`
107118

108-
Add a button to the `Detail` component, which simply triggers the globally exposed `$navigateBack` function.
119+
In the `Detail` component, add a button that triggers the globally exposed `$navigateBack` function.
109120

110-
```vue
121+
```Vue
111122
const Detail = {
112123
template: `
113124
<Page>
@@ -122,12 +133,55 @@ const Detail = {
122133

123134
### `$showModal`
124135

125-
If you want to show the `Detail` page modally, simply replace `$navigateTo` by `$showModal`.
126-
As before, you can call this method either from the view or a function.
136+
Use `$showModal` to show the `Detail` page modally. This function behaves similarly to `$navigateTo`.
127137

128-
To close the modal, call `$modal.close`.
138+
You can call `$showModal` in the view or in a method. To close the modal, call `$modal.close`.
129139

130-
```vue
140+
#### In the view
141+
142+
In the `Master` component, use a `data` property to expose the `Detail` component. Invoke `$showModal(<propertyName>)` in the view directly.
143+
144+
```Vue
145+
const Vue = require('nativescript-vue');
146+
147+
const Master = {
148+
template: `
149+
<Page>
150+
<ActionBar title="Master" />
151+
<StackLayout>
152+
<Button text="To Details directly" @tap="$showModal(detailPage)" />
153+
</StackLayout>
154+
</Page>
155+
`,
156+
157+
data() {
158+
return {
159+
detailPage: Detail
160+
}
161+
}
162+
};
163+
164+
const Detail = {
165+
template: `
166+
<Page>
167+
<ActionBar title="Detail"/>
168+
<StackLayout>
169+
<Button @tap="$modal.close" text="Close" />
170+
</StackLayout>
171+
</Page>
172+
`
173+
};
174+
175+
new Vue({
176+
render: h => h(Master)
177+
}).$start()
178+
```
179+
180+
#### In a method
181+
182+
Bind a button to a method and use `this.$showModal(Detail)` to navigate to the `Detail` component.
183+
184+
```Vue
131185
const Master = {
132186
template: `
133187
<Page>
@@ -159,15 +213,13 @@ const Detail = {
159213

160214
#### Passing props to the modal
161215

162-
Properties can be passed to the modal by including `propsData` inside a `context` object passed as an option when calling `$showModal`.
163-
164-
If we were to pass an `id` prop to the Detail component from the previous Master/Detail example, we would show the modal using:
216+
`$showModal` accepts a second parameter. You can use the parameter to pass in a `context` object containing `propsData` to the target component. For example:
165217

166-
```js
218+
```JavaScript
167219
this.$showModal(Detail, { context: { propsData: { id: 14 }}});
168220
```
169221

170-
The Detail component also has to be updated to be able to accept the `id` prop. This is done by defining a `props` option inside the component:
222+
You also need to update the `Detail` component to be able to accept the `id` prop. You can do this by defining a `props` option inside the component:
171223

172224
```vue
173225
const Detail = {
@@ -184,22 +236,22 @@ const Detail = {
184236
};
185237
```
186238

187-
[Read more about props in the official Vue documentation](https://vuejs.org/v2/guide/components-props.html)
188-
189239
The prop is now accessible throughout the component with `this.id`.
190240

241+
For more information about props, see the [official Vue documentation](https://vuejs.org/v2/guide/components-props.html)
242+
191243
#### Returning data from the modal
192244

193245
When calling `$showModal`, a promise is returned which resolves with any data passed to the `$modal.close` function.
194246

195-
For example:
247+
In the following example, closing the modal outputs 'Foo' in the console.
196248

197-
```js
249+
```JavaScript
198250
// ... inside Master
199-
this.$showModal(Detail).then(data => console.log(data)); // Will output 'Foo'
251+
this.$showModal(Detail).then(data => console.log(data));
200252
```
201253

202-
```html
203-
// ... inside Detail
254+
```HTML
255+
<!-- inside Detail -->
204256
<Button @tap="$modal.close('Foo')" text="Close" />
205257
```

0 commit comments

Comments
 (0)