Skip to content

Routing guard for parameter changes #1577

@mitar

Description

@mitar

What problem does this feature solve?

I am trying to implement TodoMVC with Vue router for practice. I have defined the following route:

{
  path: '/:visibility?',
  name: 'todos-display',
  component: Vue.component('todos-display'),
  props: true,
  beforeEnter: (to, from, next) => {
    if (!to.params.visibility || ['all', 'active', 'completed'].includes(to.params.visibility)) {
      next();
    }
    else {
     next('/');
    }
  },
}

And in the todos-display component I have the following prop defined:

props: {
  visibility: {
    type: String,
    default: 'all',
    validator: (value) => {
      return ['all', 'active', 'completed'].includes(value);
    },
  },
}

The goal of this is that the component should not care about routing or use routing information, or try to resolve invalid parameters. It should be used correctly and with only given values. On the other hand, I want router to assure that only correct values are sent down to the component. I have defined beforeEnter which works well for initial URL, but because visibility is a parameter, it is not run when parameters change, so when parameter changes, the component gets invalid value.

What does the proposed API look like?

I would suggest to add beforeRouteUpdate hook to routes which would be called before parameters change and allow checking for their values in the place which should contain this logic (the route) and not in the component (which should just know about props).

beforeRouteUpdate would otherwise behave identically to beforeEnter in arguments and semantics.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions