Skip to content

Pass propsData in beforeRouteEnter's next callback and call it before mounting #992

@rhyek

Description

@rhyek

So, after reading #779, #714 and running some tests of my own, I understand that the order of execution when using beforeRouteEnter is:

  1. beforeRouteEnter
  2. next
  3. All component's lifecycle hooks starting from beforeCreate down to mounted
  4. 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'
      }
    });
  }
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions