-
-
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
Pass propsData in beforeRouteEnter's next callback and call it before mounting #992
Comments
A couple of thoughts:
|
is there any progress ? |
No, right now the next callback waits for the next tick, that's why it's only called after the mounted hook |
@posva: The next callback gets called after mounted but if we pass an Object as argument there's no need to put it at the end of the event loop and could resolve the async loading problem:
BUT! We than we need also a |
Btw, this also works actually: props: function(to) {
console.log('PROPS', to.meta.props);
return to.meta.props;
},
beforeEnter(to, from, next) {
console.log('before enter', to.meta);
setTimeout(function() {
to.meta.props = {
id: to.params.id,
test: "123"
};
next();
},500); |
@psi-4ward: Very interesting! It would kind of make route definitions rather verbose going with this approach, though. I feel like the general preference is in-component guards for this sort of thing. Kind of off-topic, but I like Ember's approach where the route class has a Returning promises allows for much cleaner code, IMO. Something like this would be cool: export default {
async model (to) {
const { data } = await axios.get(`/api/items/${ to.params.id }`)
return { item: data }
},
props: {
item: {
type: Object,
required: true
}
} Edit: Any error raised inside |
I wrote a tiny module which does things with SSR |
As this is something that can be written in userland in a helper function and we don't want to overcomplicate apis and give even more solutions around data fetching, we will pass on this |
So, after reading #779, #714 and running some tests of my own, I understand that the order of execution when using
beforeRouteEnter
is:beforeRouteEnter
next
beforeCreate
down tomounted
next
's callback (which usually sets data on the vm)So, if you're trying to display a nested property on your data object, you need to initialize it, otherwise you'll get runtime errors (e.g., cannot read property of null).
Sometimes it may be a little complicated/tedious to initialize data when it's structure is expected to be deep/complex, but it's often necessary if you're directly accessing nested properties in your template or computed properties. This can of course also be avoided by having something like
<span v-if="person">{{ person.name}}</span>
, but it also makes your markup more verbose.I feel like the way
beforeRouteEnter
should work, is after you run your async operations and have your data ready, you should be able to somehow passpropsData
during the vm creation. This would feel more natural to me, since data is coming from outside and these components would be guaranteed their data is already there when it renders.I'd love to be able to do something like this:
The text was updated successfully, but these errors were encountered: