Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Router middleware #2928

Closed
VasiliyRusin opened this issue Sep 13, 2019 · 1 comment
Closed

Router middleware #2928

VasiliyRusin opened this issue Sep 13, 2019 · 1 comment

Comments

@VasiliyRusin
Copy link

What problem does this feature solve?

Simple middleware out of the box. The idea and realization were shown in this article https://markus.oberlehner.net/blog/implementing-a-simple-middleware-with-vue-router/
And I think it's really good solutions for router middleware.

If it can be realized by existing "beforeEach" hook, Is it really necessary? Yes. It's a beautiful and DRY way to check user authentification or page access rights, logging or A/B tests. Also, it isn't breaking existing code.

What does the proposed API look like?

Example from the article. Maybe the object "meta" isn't necessary. It should be discussed.

 import Vue from 'vue';
 import Router from 'vue-router';
 
 import auth from './middleware/auth';
 import log from './middleware/log';
 
 import Home from './views/Home.vue';
 import Login from './views/Login.vue';
 import User from './views/User.vue';

 Vue.use(Router);

 const router = new Router({
   routes: [
     {
       path: '/',
       name: 'home',
       component: Home,
       meta: {
         middleware: log,
       },
     },
     {
       path: '/login',
       name: 'login',
       component: Login,
       meta: {
         middleware: log,
       },
     },
     {
       path: '/user',
       name: 'user',
       component: User,
       meta: {
         // Hint: try to switch those two around to see
         // how this affects execution of the callbacks.
         middleware: [auth, log],
       },
     },
   ],
   mode: 'history',
 });

 export default router;
@posva
Copy link
Member

posva commented Sep 13, 2019

Duplicate of #2688

@posva posva marked this as a duplicate of #2688 Sep 13, 2019
@posva posva closed this as completed Sep 13, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants