Eren Sönmez
Eren Sönmez

Reputation: 67

Create component in template from data variable

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.

Upvotes: 0

Views: 195

Answers (1)

Igor Moraru
Igor Moraru

Reputation: 7739

Use the component tag.

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

See the Docs

Upvotes: 3

Related Questions