Home > Mobile >  Create component in template from data variable
Create component in template from data variable

Time:11-20

I have multiple components that take same props. I want to use those components in template in such a way that I don't use multiple if-else statements in my template. I created an object in my data and paired my components with string keys. Is there a way to call those components in template with that object ? My data object looks something like this:

componentMap:{
  "testComponent1":TestComponent1,
  "testComponent2":TestComponent2,
},

For example, if I give "testComponent1" as key, then in template it should use TestComponent.

CodePudding user response:

Use the component tag.

<component :is="componentMap['testComponent1']"></component>

See the Docs

  • Related