Skip to content

Providing the router instance as THIS for router guards "beforeEach" and "beforeRouteEnter" #2118

Closed
@tmcdos

Description

@tmcdos

What problem does this feature solve?

In the current version of the router (3.0.1) the global guard beforeEach and the component guard beforeRouteEnter do not provide a valid THIS value. Obviously for beforeEach if you want to access the app property of the router you need a valid THIS in the hook. And since the component is not yet instantiated in beforeRouteEnter you can not access the component as THIS - but at least you should be able to access the router instance (and its app property).

What does the proposed API look like?

I am proposing the following 2 simple changes in the code (file /src/history/base.js):

  1. in function confirmTransition() - invoke the hook with the router instance as THIS
    this.pending = route
    const iterator = (hook: NavigationGuard, next) => {
      if (this.pending !== route) {
        return abort()
      }
      try {
        hook.call(this, route, current, (to: any) => { /// <----- proposed change
  1. in function bindEnterGuard() - invoke the hook with the router instance as THIS
  return function routeEnterGuard (to, from, next) {
    return guard.call(this, to, from, cb => { /// <---- proposed change
      next(cb)
      if (typeof cb === 'function') { 

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions