Skip to content

Commit a82aa77

Browse files
committed
add docs
1 parent 686a4ec commit a82aa77

File tree

5 files changed

+141
-85
lines changed

5 files changed

+141
-85
lines changed

docs/.vuepress/config.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ module.exports = {
1313
title: 'Getting Started',
1414
collapsable: false,
1515
children: [
16-
'installation',
17-
''
16+
'',
17+
'quickstart'
1818
]
1919
},
2020
{

docs/guide/README.md

Lines changed: 13 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,20 @@
1-
# Quickstart
1+
# Introduction
22

3-
## Get Started in 3 Easy Steps
3+
A middleware is a block of code that will run when navigating
4+
from one route to another.
45

5-
1. Register middleware plugin in your app.
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 elegant way of reusing
9+
same logic in multiple routes.
610

7-
```javascript
8-
import Vue from 'vue'
9-
import MiddlewarePlugin from 'vue-router-middleware-plugin'
10-
import router from '@/path-to-router'
11+
The plugin utilizes navigation guards to implement easy to use, readable and more
12+
organized middlewares for your routes.
1113

12-
Vue.use(MiddlewarePlugin, router)
13-
```
14+
### Installation
1415

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.
28-
29-
```javascript
30-
import AuthMiddleware from '@/path-to-auth-middleware'
31-
32-
export default new VueRouter({
33-
routes: [
34-
{
35-
path: '/',
36-
meta: {
37-
middleware: AuthMiddleware
38-
},
39-
.
40-
.
41-
},
42-
```
43-
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-
},
16+
```bash
17+
npm i -S vue-router-middleware-plugin
5518
```
5619

57-
Easy as that to get started. For advanced features continue to [configurations](configurations).
20+
Get started with the quickstart guide.

docs/guide/installation.md

Lines changed: 0 additions & 18 deletions
This file was deleted.

docs/guide/middlewares.md

Lines changed: 69 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Middlewares
1+
# Middleware
22

33
A middleware is a function which accepts a [context](context) object as the only
44
argument.
@@ -27,11 +27,13 @@ There are two types of middlewares:
2727
1. [Route Middleware](#route-middleware)
2828
2. [Global Middleware](#global-middleware)
2929

30-
### Route Middleware
30+
## Route Middleware
3131

32-
1. Attach a middleware to your route
32+
These middlewares are attached to a route inside the route object.
3333

3434
```javascript
35+
import DashboardMiddleware from '@/path-to-middleware'
36+
3537
new VueRouter({
3638
routes: [
3739
{
@@ -45,11 +47,19 @@ new VueRouter({
4547
})
4648
```
4749

48-
Now the `DashboardMiddleware` is attached to route. When the user
50+
Now the `DashboardMiddleware` is attached to the route. When the user
4951
navigates to `/dashboard`, the middleware function will be resolved before entering
5052
the route.
5153

54+
You can also attach multiple middlewares to a route.
55+
5256
```javascript
57+
import ProfileMiddleware from '@/path-to-middleware'
58+
import AnalyticsMiddleware from '@/path-to-analytics-middleware'
59+
60+
.
61+
.
62+
5363
{
5464
path: '/profile',
5565
name: 'profile',
@@ -59,7 +69,40 @@ the route.
5969
},
6070
```
6171

72+
## Global Middleware
73+
74+
As the name suggests they are globally attached to all the routes of your app.
75+
76+
```javascript
77+
Vue.use(MiddlewarePlugin, {
78+
router,
79+
middleware: AuthMiddleware
80+
})
81+
82+
// or
83+
84+
Vue.use(MiddlewarePlugin, {
85+
router,
86+
middleware: [AuthMiddleware, AnalyticsMiddleware]
87+
})
88+
```
89+
90+
Now, the `AuthMiddleware` and `AnalyticsMiddleware` will be resolved before each
91+
route of your app.
92+
93+
If you want, you can ignore any global middleware in any route.
94+
6295
```javascript
96+
{
97+
path: '/contact',
98+
name: 'contact',
99+
meta: {
100+
middleware: {
101+
ignore: AuthMiddleware,
102+
attach: AnalyticsMiddleware
103+
}
104+
}
105+
},
63106
{
64107
path: '/about',
65108
name: 'about',
@@ -73,18 +116,29 @@ the route.
73116
]
74117
```
75118

76-
## Global Middleware
119+
Any middleware of property `igonre` will be skipped by the middleware pipeline.
120+
This include both global and route middlewares.
77121

78-
```javascript
79-
Vue.use(MiddlewarePlugin, {
80-
router,
81-
middleware: AuthMiddleware
82-
})
122+
You can attach a middleware to a route using `attach`.
83123

84-
// or
124+
## Middleware Pipeline
85125

86-
Vue.use(MiddlewarePlugin, {
87-
router,
88-
middleware: [AuthMiddleware, AnalyticsMiddleware]
89-
})
126+
The middleware pipeline is a queue of middlewares which needs to be resolved before
127+
entering a route. All Middlewares will be resolved sequentially in order. For example,
128+
129+
```javascript
130+
{
131+
path: '/profile',
132+
name: 'profile',
133+
meta: {
134+
middleware: [ProfileMiddleware, AnalyticsMiddleware]
135+
}
136+
},
90137
```
138+
139+
In this route, `ProfileMiddleware` will be resolved before `AnalyticsMiddleware`.
140+
141+
Global Middlewares have precendence over route middlewares i.e. they will be resolved
142+
sequentially in order before any route middlewares.
143+
144+
:+1: Thank you, hope you find this plugin useful.

docs/guide/quickstart.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Quickstart
2+
3+
## Get Started in 3 Easy Steps
4+
5+
1. Register middleware plugin in your app.
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. 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.
28+
29+
```javascript
30+
import AuthMiddleware from '@/path-to-auth-middleware'
31+
32+
export default new VueRouter({
33+
routes: [
34+
{
35+
path: '/',
36+
meta: {
37+
middleware: AuthMiddleware
38+
},
39+
.
40+
.
41+
},
42+
```
43+
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 [Advanced Guide](configurations).

0 commit comments

Comments
 (0)