Billal BEGUERADJ
Billal BEGUERADJ

Reputation: 22804

How to loop over components object in the template?

Usually we define in a Nuxt.js component something like this:

<script>
export default {
 components: {
    // components
  }
 data() {
   return {
     // key/value data
   }
 },
 methods: {
    // method definitions
 }
}
</script>

Is there a way to read the components object as we read data() and methods ?

This is because I have several components and I want to loop on them to refactor parts of my code.

Upvotes: 1

Views: 198

Answers (1)

Pratik Patel
Pratik Patel

Reputation: 6978

You can get Component data by using $options. Try this.

created() {
      console.log(this.$options.components)
}

it returns an object, keys are the component names, values are the contructors.

codepen - https://codesandbox.io/s/yk9km5m0wv

Upvotes: 2

Related Questions