-
-
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
Resolving async data in router with async props #1923
Comments
At first, I was quite skeptical because it's adding another way of doing something |
I stumbled on this problem today. This is badly needed: I assumed it was already implemented. |
Any news regarding this? I would also like to resolve data before passing it to the view (angular had this with resolve, it would be quite handy to have something like it without depending on the store) |
I'm also struggling with this, @FrEaKmAn's first pass is good but as he detailed it is also incomplete. This is the only thing preventing us from a full move away from Angular1. |
To be clear, async data is already feasible in the current state of vue-router and it is documented. This feature would make things more declarative and easier to test |
@posva where this is documented, can you provide the link? |
Hey @FrEaKmAn, Check out the repo for a small demo: https://github.com/AminZoubaa/vue-async-router-demo @wlkns maybe that could help you too |
Here are my 2 cents: # Todolist.vue
<template>
<ul>
<li v-for="todo in todos" :key="todo.id">
{{ todo.text }}
</li>
</ul>
</template>
<script>
export default {
props: {
todos: {
type: Array,
required: true,
},
},
}
</script> # router.js
import VueRouter from "vue-router";
const hydrateProps = (route, props) => {
Object.assign(route.meta, {props});
}
return new VueRouter({
routes: [
{
path: '/todos',
name: 'todolist',
async beforeEnter(to, from, next) {
const todos = await someService.retrieve('/todos');
hydrateProps(to, {todos});
next();
},
props: (route) => ({
todos: route.meta.props.todos
}),
component: () => import('Todolist'),
}
],
}); WDYT? |
After further consideration and after revisiting the architecture of navigation guards, props cannot be async because they are a mere way to provide data to components and must be synchronous. They are not part of the navigation flow. It's however easy to recreate a similar behavior in Vue Router 4 by storing data in |
@posva [In Vue router 3, changes to to.meta should be avoided. One could ...], you never finished this sentence, and I am using Vue router 3 |
What problem does this feature solve?
Idea it to handle all async data in the router stage, so the component already get's the data without a need for v-if checking. vue-router already has
beforeRouteEnter
andbeforeRouteEnter
, but I think it's too complicated because I need to reimplement both methods to be able to reuse component, the code overhead is to large and it also doesn't enable me to reuse component (same component, different data source).What does the proposed API look like?
API proposes to add additional logic into router beforeEach (this is always called). Changes to router are
and then we simply make our props async
This will first resolve the data, then render the component. All data will be passed to component through standard
props
.Of course, there is additional works to handle all types of props and to have dependency resolves (I resolve something based on previous resolve).
The text was updated successfully, but these errors were encountered: