How to use controller in vuejs2

softya :

Hi I start using vuejs2 with project based on laravel backend in my vuejs2, I did this code in the file routes.js

export default new VueRouter({
  routes: [{
      path: '/test',
      component: Test
    },
    {
      path: '/settings',
      component: Settings
    },
    // users
    {
      path: '/users/create',
      component: Create
    },
    // end users
  ],
});

Now in the line { path: '/users/create', component: Create}, I'm wondering how could I go to controller first and the controller redirect me to the component like in laravel first think I write in route.php and calling a function in the controller and the controller redirect me to the view vile what I found that's from the route I go to the component but I need to do things before redirecting me to the component thanks a lot.

Dhaval Chheda :

. There is a guard available with VueRouter to perform checks. It is called BeforeEach(). What this will do is before resolving the next request in the pipeline it will check for the conditions to be satisfied.

const router = new VueRouter({ ... })

router.beforeEach((to, from, next) => {
  if(some_condition){
      next();
  } else if(some_error){
       next('/'); //rerouting
  }
})

You can check this documentation : https://router.vuejs.org/guide/advanced/navigation-guards.html#global-before-guards

Here you can push the route you want to include in the pipeline using next() and change it according to your conditions.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=15572&siteId=1