-
-
Notifications
You must be signed in to change notification settings - Fork 5.1k
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
Add afterRouteEnter guard #2840
Comments
I think adding an after guard in components is not a good idea because it adds more cognitive load for people learning and because it's something that most of the time can be refactored in a method that is called in both The only thing is, other nested guards can be executed and reject navigation and this can cause some code executed in a So, I'm interested about use cases for an in-component after hook Regarding the callback passed to |
Oh, my apologies. did not see this bug.
Looks like working (at least a quick test showed so). The case with the in-component after hook is: Yes, these parameters can be calculated even on the server or in asyncData and implement a 302 redirect ... You ask the purpose of these parameters? So what do we have? Well, in general, I was very surprised that the component cannot be set to callback for execution after. Oh, and I forgot - the main problem was that my function of replacing parameters worked before the transition of the router was completed |
No, no, it's a caveat of the solution I described about putting the afterRouteEnter code inside the
But then that should be doable in a global But is there any reason to access the component in that after hook? If not, a global hook is the right place About the query parameters, I think it would make more sense to allow them not to exist in the URL by providing default values. Something like using computed properties: computed: {
limit: {
get: ({ $route }) => Number($route.query.limit) || 30,
set (limit) {
this.$router.push({ query: { ...this.$route.query, limit })
}
}
} |
Nuxt... no, i don't using this. I have custom ssr solution from one example with koa.
Yes! This logic with parameters applies only to one route. If i write everything in the global after hook, then its code will sooner or later become huge. With query parameters, yes, I do: These values are stored in the store because I have a huge logic of navigating this route, receiving data, many more related entities (bread crumbs), filters, and so on. |
So there is no need to access the component itself. A global after is perfectly fine, you can filter inside it: const routes = [
{
path: '/...',
// other options
meta: { queryRedirect: { ... } }
}
]
router.afterEach((to, from, next) => {
// could also be a check about the name
if (!to.meta.queryRedirect) return next()
if (!isQueryPresent(to.query)) next({ query: to.meta.queryRedirect }) // using spread to create a copy
else next()
}) Regarding where to store the data, I would still store the data in the url and let the store retrieve through a getter so there is only one source of truth I still believe having a default value instead of creating a redirection is an easier way to solve the problem |
I need access to component itself for access store. About global afterEach:
My asyncData takes 2 params: store and route there 1 place where i can access to both. Let me try to describe the algorithm as it works in my project:
the following happens on the client:
Default value that you write - creating redirection too. And several default values will create more redirects. |
But that's my point, the store can be accessed globally. For example in Nuxt, you have access to a context in multiple places and that's how you can access both the store and router in plugins. So, do you need access to anything else beyond the store? Thanks for sharing the way you do it. It's okay to have an |
Ok, i'll try yo do with afterEach and try to make my store accessible from everywhere. But, by the way, I have a project - a template of what I described above. I can add a little of it to repeat the problem and write a link to the repository |
I appreciate it but there is no need since there isn't a bug to check |
See vuejs/rfcs#150 |
What problem does this feature solve?
I want execute one function in component after each routing (enter route, reused in the new route)
What does the proposed API look like?
variant 1:
variant 2: add hook named "afterRouteUpdate" that will calling after we enter in route and after each time we navigating in this route (foo/2 -> foo/3)
The text was updated successfully, but these errors were encountered: