[Vuejs]-Get which position a child has in its parent in Vue JS

0👍

It depends on why do you need this information: if you need some specific styles for nth component, it’s better to use :nth-child or :nth-of-type CSS selectors.
Passing the position number within a prop is also a nice way to do it.

You also may refer to $parent and iterate over its children to find the current element, like [...this.$parent.$el.children].findIndex(child => child === this.$el) (make sure both parent and child have already been mounted), but this code is quite fragile so use it at your own risk.

Leave a comment