π Vue Dynamic Components β Switch Between Components on the Fly (2025 Guide)
π§² Introduction β What Are Dynamic Components in Vue?
Dynamic components in Vue.js allow you to render different components dynamically based on runtime conditions. This is achieved using the <component :is="componentName" />
tag, which acts as a placeholder for changing componentsβmaking it ideal for tab views, wizards, modals, and dashboards.
π― In this guide, youβll learn:
- How dynamic components work in Vue
- The syntax for switching components using
<component :is="...">
- Real-world use cases
- Best practices and performance tips
π What Is a Dynamic Component?
A dynamic component allows Vue to render a component type determined at runtime rather than hardcoding it in the template.
β Basic Syntax:
<component :is="currentComponent"></component>
Here, currentComponent
is a variable that holds the name or object reference of a component.
π§± Example β Switching Components Dynamically
β App.vue
<template>
<div>
<button @click="currentComponent = 'Home'">Home</button>
<button @click="currentComponent = 'About'">About</button>
<component :is="currentComponent" />
</div>
</template>
<script>
import Home from './components/Home.vue';
import About from './components/About.vue';
export default {
components: { Home, About },
data() {
return {
currentComponent: 'Home'
};
}
}
</script>
π§ The <component>
tag switches between <Home />
and <About />
based on the button clicked.
𧬠Using Object References Instead of Strings
You can also pass actual component objects:
<component :is="componentObject" />
data() {
return {
componentObject: Home
};
}
π¦ Real-World Use Case β Tab Interface
β Tab Components
<Tabs>
<template #tabs>
<button @click="tab = 'TabA'">A</button>
<button @click="tab = 'TabB'">B</button>
</template>
<component :is="tab"></component>
</Tabs>
components: {
TabA, TabB
},
data() {
return {
tab: 'TabA'
};
}
π§ This creates a dynamic tabbed interface without repeated markup.
π§° Dynamic Component with <keep-alive>
Vueβs <keep-alive>
tag caches inactive components to preserve state when switching.
β Example:
<keep-alive>
<component :is="currentComponent" />
</keep-alive>
π§ Preserves data, scroll position, and input values in inactive components.
βοΈ Dynamic Components vs Conditional Rendering
Feature | Dynamic Component | v-if /v-else Rendering |
---|---|---|
Syntax | <component :is="..." /> | <div v-if="...">...</div> |
Reusability | β High | β Limited to static markup |
State Preservation | β
(with <keep-alive> ) | β No |
Use Cases | Tabs, forms, modals | Simple toggling or one-time |
π« Common Mistakes
β Mistake | β Solution |
---|---|
Using undefined component names | Always register components properly |
Forgetting to wrap with <keep-alive> | Wrap only if you want to preserve state |
Hardcoding logic in the template | Use data-bound variables for clean switching |
π Summary β Recap & Next Steps
Dynamic components allow Vue developers to switch views flexibly using runtime logic. It improves modularity, enhances UX with persistent state, and enables powerful UI patterns like tabs, step wizards, and dashboards.
π Key Takeaways:
- Use
<component :is="...">
to switch components at runtime - Use strings or component object references
- Wrap in
<keep-alive>
to retain state - Ideal for tabs, dynamic forms, and modular views
βοΈ Real-World Relevance:
Dynamic components power modern SPAs, content editors, multi-step forms, and component-driven UIs where flexibility is key.
β FAQ Section
β What is a dynamic component in Vue?
β Itβs a Vue feature that renders a component based on the value of a variable using:
<component :is="componentName" />
β Can I pass component objects instead of strings?
β
Yes. Instead of 'Home'
, you can pass Home
directly:
<component :is="Home" />
β How do I retain state while switching components?
β
Use <keep-alive>
:
<keep-alive>
<component :is="current" />
</keep-alive>
β Are dynamic components better than v-if
?
β
For complex views with reusability and state retentionβyes. Use v-if
for simple logic.
β Can I register dynamic components globally?
β
Yes. Use app.component('ComponentName', Component)
in main.js and pass the string name to <component :is="...">
.
Share Now :