Skip to content

Commit 5708444

Browse files
committed
update docs
1 parent 969b487 commit 5708444

File tree

5 files changed

+168
-32
lines changed

5 files changed

+168
-32
lines changed

.markdownlintrc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"MD036": false,
3+
"MD001": false
4+
}

docs/guide/README.md

Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,30 @@
11
# Quickstart
22

3-
```javascript
4-
import Vue from 'vue'
5-
import MiddlewarePlugin from 'vue-router-middleware-plugin'
6-
import router from '@/path-to-router'
3+
## Get Started in 3 Easy Steps
74

8-
Vue.use(MiddlewarePlugin, router)
9-
```
5+
1. Register middleware plugin in your app.
106

11-
```javascript
12-
import store from '@/path-to-store'
7+
```javascript
8+
import Vue from 'vue'
9+
import MiddlewarePlugin from 'vue-router-middleware-plugin'
10+
import router from '@/path-to-router'
1311

14-
export default ({ to, from, redirect }) => {
15-
if (!store.getters.isLoggedIn) {
16-
redirect('/login')
17-
}
18-
}
19-
```
12+
Vue.use(MiddlewarePlugin, router)
13+
```
14+
15+
2. Create a middleware function.
16+
17+
```javascript
18+
import store from '@/path-to-store'
19+
20+
export default ({ to, from, redirect }) => {
21+
if (!store.getters.isLoggedIn) {
22+
redirect('/login')
23+
}
24+
}
25+
```
26+
27+
3. Attach middleware to a route.
2028

2129
```javascript
2230
import AuthMiddleware from '@/path-to-auth-middleware'
@@ -31,16 +39,19 @@ export default new VueRouter({
3139
.
3240
.
3341
},
34-
{
35-
path: '/login',
36-
meta: {
37-
middleware: [LoggerMiddleware, AnalyticsMiddleware]
38-
},
39-
.
40-
.
41-
}
42-
]
43-
})
4442
```
4543

46-
Easy as that to get started. For advanced features continue to [configurations](/configurations).
44+
Note: You may attach multiple middlewares to a route.
45+
46+
```javascript
47+
{
48+
path: '/login',
49+
meta: {
50+
middleware: [LoggerMiddleware, AnalyticsMiddleware]
51+
},
52+
.
53+
.
54+
},
55+
```
56+
57+
Easy as that to get started. For advanced features continue to [configurations](configurations).

docs/guide/configurations.md

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,67 @@
11
# Configuration
22

3-
```bash
4-
npm install --save vue-router-middleware-plugin
3+
The plugin can be configured in multiple ways with the help of plugin options.
4+
5+
#### 1. Simple Configuration
6+
7+
```javascript
8+
import Vue from 'vue'
9+
import MiddlewarePlugin from 'vue-router-middleware-plugin'
10+
import router from '@/path-to-router'
11+
12+
Vue.use(MiddlewarePlugin, router)
13+
```
14+
15+
#### 2. Custom [Middleware Context](context)
16+
17+
```javascript
18+
import Vue from 'vue'
19+
import MiddlewarePlugin from 'vue-router-middleware-plugin'
20+
import router from '@/path-to-router'
21+
import store from '@/path-to-store'
22+
23+
Vue.use(MiddlewarePlugin, {
24+
router,
25+
context: { store }
26+
})
27+
```
28+
29+
Now, `store` will be injected as a property in all middleware contexts.
30+
31+
```javascript
32+
export default async ({ store }) => {
33+
await store.dispatch('app/getData')
34+
}
35+
```
36+
37+
#### 3. Single [Global Middleware](middlewares.html#global-middleware)
38+
39+
```javascript
40+
import Vue from 'vue'
41+
import MiddlewarePlugin from 'vue-router-middleware-plugin'
42+
import router from '@/path-to-router'
43+
import AuthMiddleware from '@/path-to-auth-middleware'
44+
45+
Vue.use(MiddlewarePlugin, {
46+
router,
47+
middleware: AuthMiddleware
48+
})
49+
```
50+
51+
If you are wondering, it is possible to ignore a global middleware for individual
52+
routes.
53+
54+
#### 4. Multiple [Global Middlewares](middlewares.html#global-middleware)
55+
56+
```javascript
57+
import Vue from 'vue'
58+
import MiddlewarePlugin from 'vue-router-middleware-plugin'
59+
import router from '@/path-to-router'
60+
import AuthMiddleware from '@/path-to-auth-middleware'
61+
import LoggerMiddleware from '@/path-to-logger-middleware'
62+
63+
Vue.use(MiddlewarePlugin, {
64+
router,
65+
middleware: [AuthMiddleware, LoggerMiddleware]
66+
})
567
```

docs/guide/context.md

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,63 @@ export default context => {
1414

1515
## Custom Context
1616

17-
It always contain a few built-in properties but can also be extended to include [custom properties](/api/context.html#adding-custom-properties) as per use case.
17+
The context can be extend by adding custom properties in plugin options.
18+
19+
```javascript
20+
import Vue from 'vue'
21+
import MiddlewarePlugin from 'vue-router-middleware-plugin'
22+
import router from '@/path-to-router'
23+
import store from '@/path-to-store'
24+
25+
Vue.use(MiddlewarePlugin, {
26+
router,
27+
context: { store }
28+
})
29+
```
30+
31+
Now, `store` will be injected as a property in all middleware contexts.
32+
33+
```javascript
34+
export default async ({ store }) => {
35+
await store.dispatch('app/getData')
36+
}
37+
```
38+
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.
43+
44+
### Dynamic Custom Context
45+
46+
The custom context properties can be mutated with the helper functions exposed to
47+
the middleware via `app` context. The ability to mutate the custom context properties
48+
makes it possible to use these properties as middleware state.
49+
50+
Initialize the state in plugin options.
51+
52+
```javascript
53+
import Vue from 'vue'
54+
import MiddlewarePlugin from 'vue-router-middleware-plugin'
55+
import router from '@/path-to-router'
56+
57+
Vue.use(MiddlewarePlugin, {
58+
router,
59+
context: { dashboardVisits: 0 }
60+
})
61+
```
62+
63+
Mutate `dashboardVisits` in any middleware function.
64+
65+
```javascript
66+
export default ({ app, to, dashboardVisits }) => {
67+
if (to.path === '/dashboard') {
68+
app.$setMiddlewareContext({ dashboardVisits: dashboardVisits + 1 })
69+
}
70+
}
71+
```
72+
73+
> You cannot mutate built-in context properties. Like `redirect`, `from` etc.
74+
75+
Goto [API References](/api/context) to find out more about helper functions and
76+
`app` context.

src/install.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,13 @@ export const install: Install<Router | PluginOptions> = (
6565
return _context
6666
}
6767
vue.$setMiddlewareContext = (_context: any): any => {
68-
const { app } = context
69-
context = { ..._context, app }
68+
const { app, to, from, redirect } = context
69+
context = { ..._context, app, to, from, redirect }
7070
return context
7171
}
7272
vue.$updateMiddlewareContext = (key: string, value: any) => {
73-
const { app } = context
74-
context = { [key]: value, app }
73+
const { app, to, from, redirect } = context
74+
context = { [key]: value, app, to, from, redirect }
7575
}
7676

7777
/* istanbul ignore next */

0 commit comments

Comments
 (0)