VUE ROUTER MIDDLEWARE
Create multiple validations in vue router with middlewares
Instalation
with npm
npm install --save vue-router-middleware
with yarn
yarn add vue-router-middleware
Get Started
Install it as a Vue Plugin like this.
const Vue from 'vue' const VueRouter from 'vue-router' const VueRouterMiddleware from 'vue-router-middleware' const routes = // ... // Create your route schema here. const router = routes Vue // Install it by pass your router instance to be an option argument. Vue router
And create middleware
const createMiddleware from 'vue-router-middleware' const FakeAuth = { return true } // Pass middleware name and callback function
with Vue.use
Vue.use(VueRouterMiddleware, { router, middlewares: { // Convert to camelcase to dash string ex. requireAuth saves require-auth requireAuth(params, to, from, next) { // Logic here }, checkPermission(params, to, from, next) { // Get params next( params.includes('super-user') ) } } })
Set middleware in routes
Use Spread Operator with middleware()
to make more simple and pretty
const routes = ... // Route without middlewares path: '/login' name: 'login' component: LoginComponent const router = routes Vue Vue
Capture on middleware is canceled
const VueRouter from 'vue-router' const VueRouterMiddleware from 'vue-router-middleware' const router = routes Vue Vue
Simple, a right?
You need more ? Full documents here
License
MIT Copyright (c) Andrey Dias