vue-router: beforeRouteUpdate never calls next callback

Version

2.7.0

Reproduction link

http://jsfiddle.net/znmLks2w/2/

Steps to reproduce

Create a component with a beforeRouteUpdate guard that passes a callback to the next function.

What is expected?

The callback function should be called.

What is actually happening?

The callback is never called.


In my example I have both beforeRouteEnter and beforeRouteUpdate pointing to the exact same function. beforeRouteEnter works exactly like I expect, but beforeRouteUpdate does not. Click on the links to try to add one to the displayed value and you’ll notice it works the first time but never any of the subsequent times.

If this behavior is purposeful then I would consider it a documentation bug - the docs need to be absolutely clear that next behaves differently depending on where you’re calling it from. But even as I type this I’m realizing that if that’s the case then it’s terrible design. It should work the same way regardless.

About this issue

  • Original URL
  • State: open
  • Created 7 years ago
  • Reactions: 14
  • Comments: 17 (7 by maintainers)

Commits related to this issue

Most upvoted comments

What solution are you guys using now? I had to dupe my code to do same thing in both beforeRouteEnter + beforeRouteUpdate

While updating the docs may help a few people who pay close attention understand what’s happening better, it does not actually solve the problem, which is that developers expect a framework like Vue to provide a consistent interface. Just because the callback is unnecessary in some cases is not a good reason to make the same function behave differently depending on where it’s called from. With two varying implementations that any reasonable person would expect to do the same thing, it means that the confusion is going to persist even if it’s spelled out clearly in the docs. Plus, it also means that every developer who runs into this has to re-invent the wheel and figure out a way to detect and handle this situation.

Let me illustrate. Given this setup:

component1 = {
    beforeRouteEnter: myFunction
}
component2 = {
    beforeRouteUpdate: myFunction
}

What seems more intuitive, natural, and easy for developers? This:

myFunction = function(from, to, next) {
    // somehow detect which hook called this function
    if (calledByEnter) {
        next(myCallback)
    }
    else {
         next()
         myCallback(this)
    }
}

Or this:

myFunction = function(from, to, next) {
    next(myCallback)
}

The situation currently requires the former. I think the people commenting in this thread - and the project as a whole - would benefit from the latter.

I’d say that this is a bug, not an improvement. Consider when using <router-view :key="...">, it becomes impossible to access the new component, so you can’t inject data. See: http://jsfiddle.net/thatguystone/nypp9jma/1/

I’d say that this is a bug, not an improvement. Consider when using <router-view :key="...">, it becomes impossible to access the new component, so you can’t inject data. See: http://jsfiddle.net/thatguystone/nypp9jma/1/

This is got me. You really don’t have access to the next component.

I resolved to fetching data in beforeRouteUpdate saving to store and then using created to get the value from store.

Update created is called asynchronously. To this point, beforeRouteUpdate and beforeRouteEnter have proved not useful due to the limitation on beforeRouteUpdate

Oh… it’s realy time for bed for me.

Ah, I see what happened 😆 This one is still opened. I personally think this is worth adding. It’s convenient to pass the same function to both hooks. I’ll close the other one

Just discovered this problem.