Vue-Route-Middleware
A simple koa-like middleware wrapper for vue-router
Caution
This module is still being developed and no guaruntee on production enviorment
Features
- Koa-like api
- Support either async or sync middleware
- A context object to store data through the whole routing lifecycle
Install
By script tag
By commonjs
npm install vue-router-middlewares -S
// orconst vueRouterWrapper =
How to use
Here is a simple snippet to show the use case
// create a vue-router instanceconst originRouter = ...// here your routes config // wrapped it with vue-route-middlewareconst router = // support chainningrouter // async middleware // sync middleware // stop routing if getting errors throw 'oops' ) router
the complete example could be found in the repo.
Context
I've delegate some params from origin vue-router instance to the ctx object passed to the middleware.
'path', 'query', 'params', 'meta', 'name', 'hash', 'matched' from to-router-instance are mapped to ctx[key]
remember they are just writeable params, if you try to set value on them, it will generate or overwrite ctx[_key]
and if you rewrite ctx.path, if the middlewares all passed, the router will change its destination like a redirecting.