-
-
Notifications
You must be signed in to change notification settings - Fork 5k
beforeRouteUpdate never calls next callback #1582
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
Comments
The difference between both hooks is that enter doesn't have access to |
I'd say that this is a bug, not an improvement. Consider when using |
I've just step on this problem. Could it be explained in the documentation on the navigation-guards page ? |
Just discovered this problem. |
Also observing this problem |
I understand your confusion, but if you look at the documentation closesly, it states that passing a callback to Both Solution: just use I will see if I can come up with a quick PR for the docs to stress this difference as this has come up a couple of times already. |
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. |
You might have a point there, I'll reopen #1676 for further discussion, because that ticket was apprearantly requesting that change as well and is newer. |
@LinusBorg what's the difference? I really see the same FR in both issues |
No difference, really. I just feel that bdoms has made a good case that should at least be discussed. I'm not convinced yet if offering a callback for all in-component hooks is actually optimal either, but it's worth to think about so I wanted to have an issue open about it, I dont really care which. We can also reopen this and I'll close the dupe again. |
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 |
Oh... it's realy time for bed for me. |
I don't understand how you are supposed to get this to work. |
What solution are you guys using now? |
Actually I'm doing the same, duplicating code in Enter and Update, I could create a global function and call it changing the target (this or vm), or somebody found another more elegant solution? |
@dariodepaolis i'm not sure it's the "best pattern" but I faced this and solved it by a mixin :
which i use in most cases like this :
(but i don't require access to I do understand the major difference between What's mainly counter-intuitive is that while |
This is got me. You really don't have access to the next component. I resolved to fetching data in Update |
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 thenext
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
andbeforeRouteUpdate
pointing to the exact same function.beforeRouteEnter
works exactly like I expect, butbeforeRouteUpdate
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.The text was updated successfully, but these errors were encountered: