@@ -10,8 +10,8 @@ const router = createRouter({
10
10
routes : [
11
11
{
12
12
path: ' /' ,
13
- name: ' home' ,
14
- component : () => import (' ./views/HomeView.vue' )
13
+ name: ' home' , // "home" will be route name
14
+ component : () => import (' ./views/HomeView.vue' ) // Always lazy load route components
15
15
},
16
16
{
17
17
path: ' /about' ,
@@ -28,12 +28,49 @@ Use `RouterView` to render the matched component for the current route, and use
28
28
<template>
29
29
<nav>
30
30
<RouterLink to="/">Home</RouterLink>
31
- <RouterLink :to="{ name: 'about' }">About</RouterLink> <!-- Named Routes -->
31
+
32
+ <!-- Use route name via object syntax -->
33
+ <RouterLink :to="{ name: 'about' }">About</RouterLink>
32
34
</nav>
33
35
<RouterView />
34
36
</template>
35
37
```
36
38
39
+ ## Programmatic Navigation
40
+
41
+ Aside from using ` <RouterLink> ` to create anchor tags for declarative navigation, we can do this programmatically using the router's instance methods.
42
+
43
+ | Declarative | Programmatic |
44
+ | ------------- | :-----------: |
45
+ | ` <RouterLink :to="..."> ` | ` router.push(...) ` |
46
+ | ` <RouterLink :to="..." replace> ` | ` router.replace(...) ` |
47
+ | ` window.history.go(n) ` | ` router.go(1) ` |
48
+
49
+ The argument can be a string path, or a location descriptor object.
50
+
51
+ ``` ts
52
+ // literal string path
53
+ router .push (' /users/eduardo' )
54
+
55
+ // object with path
56
+ router .push ({ path: ' /users/eduardo' })
57
+
58
+ // named route with params to let the router build the url
59
+ router .push ({ name: ' user' , params: { username: ' eduardo' } })
60
+
61
+ // with query, resulting in /register?plan=private
62
+ router .replace ({ path: ' /register' , query: { plan: ' private' } })
63
+
64
+ // with hash, resulting in /about#team
65
+ router .replace ({ path: ' /about' , hash: ' #team' })
66
+
67
+ // go forward by one record, the same as router.forward()
68
+ router .go (1 )
69
+
70
+ // go back by one record, the same as router.back()
71
+ router .go (- 1 )
72
+ ```
73
+
37
74
## Dynamic Routing
38
75
39
76
Dynamic routes are used to match a series of routes with some params to be acquired. (Denoted by ** colon** ` : ` )
@@ -177,41 +214,6 @@ const router = createRouter({
177
214
</template>
178
215
```
179
216
180
- ## Programmatic Navigation
181
-
182
- Aside from using ` <RouterLink> ` to create anchor tags for declarative navigation, we can do this programmatically using the router's instance methods.
183
-
184
- | Declarative | Programmatic |
185
- | ------------- | :-----------: |
186
- | ` <RouterLink :to="..."> ` | ` router.push(...) ` |
187
- | ` <RouterLink :to="..." replace> ` | ` router.replace(...) ` |
188
- | ` window.history.go(n) ` | ` router.go(1) ` |
189
-
190
- The argument can be a string path, or a location descriptor object.
191
-
192
- ``` ts
193
- // literal string path
194
- router .push (' /users/eduardo' )
195
-
196
- // object with path
197
- router .push ({ path: ' /users/eduardo' })
198
-
199
- // named route with params to let the router build the url
200
- router .push ({ name: ' user' , params: { username: ' eduardo' } })
201
-
202
- // with query, resulting in /register?plan=private
203
- router .replace ({ path: ' /register' , query: { plan: ' private' } })
204
-
205
- // with hash, resulting in /about#team
206
- router .replace ({ path: ' /about' , hash: ' #team' })
207
-
208
- // go forward by one record, the same as router.forward()
209
- router .go (1 )
210
-
211
- // go back by one record, the same as router.back()
212
- router .go (- 1 )
213
- ```
214
-
215
217
## Named Views
216
218
217
219
``` ts
@@ -304,16 +306,19 @@ const User = {
304
306
305
307
const routes = [
306
308
// Boolean mode
307
- { path: '/user/:id',
309
+ {
310
+ path: '/user/:id',
308
311
component: User,
309
312
props: true
310
313
},
314
+
311
315
// Object mode
312
316
{
313
317
path: '/user/:id',
314
318
components: { default: User, sidebar: Sidebar },
315
319
props: { default: true, sidebar: false }
316
320
},
321
+
317
322
// Function mode
318
323
{
319
324
path: '/search',
0 commit comments