-
-
Notifications
You must be signed in to change notification settings - Fork 5k
Description
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
- All component's lifecycle hooks starting from
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 pass propsData
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:
export default {
props: {
person: {
type: Object,
required: true
}
},
beforeRouteEnter(to, from, next) {
...
next({
person: {
name: 'Carlos'
}
});
}
}