Skip to content

Commit 5c80403

Browse files
committed
update docs
1 parent 5708444 commit 5c80403

File tree

3 files changed

+77
-23
lines changed

3 files changed

+77
-23
lines changed

docs/guide/context.md

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,24 @@ export default async ({ store }) => {
3636
}
3737
```
3838

39-
In this case, you could've just imported the store in the middleware instead of
40-
adding it as a custom context property but it will depend on the use case and
41-
personal preferences of a developer. One such use case will be, if you want to carry
42-
state in the middleware context.
39+
In this case, you could've imported the store in the middleware module instead of
40+
adding it as a custom context property.
41+
42+
```javascript
43+
import store from '@/path-to-store'
44+
45+
export default async () => {
46+
await store.dispatch('app/getData')
47+
}
48+
```
49+
50+
If you are thinking what's the point of adding custom context. It will depend on
51+
the use case and personal preferences of a developer. One such case might be,
52+
if you are lazy and don't want to import the same module in multiple middlewares.
53+
54+
The idea behind adding custom context was so that other plugins can leverage it
55+
to inject functionalities which can used inside a middleware, but there's more than
56+
that. With the context, you can carry state in the middleware pipeline.
4357

4458
### Dynamic Custom Context
4559

docs/guide/installation.md

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
1-
# Installation
1+
# Introduction
2+
3+
In this context, a middleware is a block of code that will run when navigating
4+
from one route to another.
5+
6+
The Vue Router offers [navigation guard API](https://router.vuejs.org/guide/advanced/navigation-guards.html)
7+
which can be used to add control logic between your routes but they can easily make
8+
your route objects messy and difficult to read and there's no easy way of reusing
9+
same logic in multiple routes.
10+
11+
The plugin utilizes navigation guards to implement easy to use, readable and more
12+
organized middlewares for your routes.
13+
14+
### Installation
215

316
```bash
4-
npm install --save vue-router-middleware-plugin
17+
npm i -S vue-router-middleware-plugin
518
```

docs/guide/middlewares.md

Lines changed: 44 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,55 @@
11
# Middlewares
22

3-
## Route Middleware
3+
A middleware is a function which accepts a [context](context) object as the only
4+
argument.
45

56
```javascript
6-
const routes = [
7-
{
8-
path: '/',
9-
name: 'home',
10-
meta: {
11-
middleware: ({ redirect }) => {
12-
redirect('/dashboard')
7+
export default ({ app, to, from, redirect }) => {
8+
console.log('from:', from, ', to:', to)
9+
}
10+
```
11+
12+
You can run a long running task in a middleware.
13+
14+
```javascript
15+
import store from '@/path-to-store'
16+
17+
export default async ({ app, to, from, redirect }) => {
18+
await store.dispatch('app/aVeryLongRunningTask')
19+
}
20+
```
21+
22+
The middleware pipeline will await for this middleware to
23+
be resolved before startng the execution of next middleware or route.
24+
25+
There are two types of middlewares:
26+
27+
1. [Route Middleware](#route-middleware)
28+
2. [Global Middleware](#global-middleware)
29+
30+
### Route Middleware
31+
32+
1. Attach a middleware to your route
33+
34+
```javascript
35+
new VueRouter({
36+
routes: [
37+
{
38+
path: '/dashboard',
39+
name: 'dashboard',
40+
meta: {
41+
middleware: DashboardMiddleware
1342
}
14-
}
15-
}
43+
},
44+
]
45+
})
1646
```
1747

48+
Now the `DashboardMiddleware` is attached to route. When the user
49+
navigates to `/dashboard`, the middleware function will be resolved before entering
50+
the route.
51+
1852
```javascript
19-
{
20-
path: '/dashboard',
21-
name: 'dashboard',
22-
meta: {
23-
middleware: DashboardMiddleware
24-
}
25-
},
2653
{
2754
path: '/profile',
2855
name: 'profile',

0 commit comments

Comments
 (0)