Skip to content
Advertisement

Why can’t I access props within my component?

I’ve copied the Grid Component Example into a single-file component (Grid.vue). Within that component, I’m not able to access the columns prop. console.log(this.columns) always prints: [__ob__: Observer] to the log. Can someone tell me why? This works fine in their example on the page and in JSFiddle.

Here’s my Grid.vue file:

JavaScript

I’m using this component within another component like so:

JavaScript

Advertisement

Answer

In:

JavaScript

The value of this.thingColumns is passed as :columns when mounting.

Thus, the console.log(this.columns) inside Grid.vue/data() prints when it is mounting.

And when it is mounting, thingColumns is empty in the parent:

JavaScript

Since the console.log(this.columns) inside Grid.vue/data() prints when it is mounting, that is, before it is mounted, it prints an empty array:

JavaScript

Because, well, parent’s thingColumns will only have data after the mounted() hook executes.

And since it is a reactive array, when you update it, it will update the child grid component as well.


Solution:

Move the property initalization code from mounted() to created():

JavaScript

This will initialize the data sooner and make it available in time for the console.log() in the child to pick it up.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement