vuejs
/

Vue Dynamic Components

Last Sync: Today

On this page

9
0%
5 min read
Remaining
5 minleft

Click any section to jump — progress syncs automatically

vuejs

Vue Dynamic Components

What are Dynamic Components?

Dynamic components allow you to switch between different components dynamically using the <component> element.

Basic Syntax

HTMLRead-only
1
<component :is="currentComponent"></component>

Example

HTMLRead-only
1
<button @click="current = 'CompA'">A</button>
<button @click="current = 'CompB'">B</button>

<component :is="current"></component>

Registering Components

JavaScriptRead-only
1
components: {
  CompA,
  CompB
}

Keep Alive

Use <keep-alive> to cache inactive components and preserve state.

HTMLRead-only
1
<keep-alive>
  <component :is="currentComponent"></component>
</keep-alive>

When to Use

  • Switching tabs dynamically
  • Conditional component rendering
  • Reusable layouts
  • Dynamic UI rendering

Best Practices

  • Use meaningful component names
  • Use keep-alive for caching
  • Avoid unnecessary switching
  • Keep component logic simple

Common Mistakes

  • Not registering components properly
  • Forgetting keep-alive when needed
  • Overusing dynamic components
  • Complex logic in switching conditions

Conclusion

Dynamic components in Vue provide flexibility in rendering different components at runtime, improving UI adaptability.

Try it yourself

<component :is="currentComponent"></component>

Test Your Knowledge

Q1
of 3

Which tag used?

A
div
B
component
C
span
D
section
Q2
of 3

What does :is do?

A
Loop
B
Condition
C
Select component
D
Bind data
Q3
of 3

Which caches component?

A
v-if
B
keep-alive
C
v-for
D
v-bind

Frequently Asked Questions

What is a dynamic component?

It allows switching components dynamically.

What is :is?

It defines which component to render.

What is keep-alive?

It caches components to preserve state.

Previous

vue slots

Next

vue lifecycle hooks

Related Content

Need help?

Explore our comprehensive docs or start a chat with our tech experts.