Vue Slots & Advanced Components
Estimated reading: 4 minutes 27 views

πŸ”„ 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

FeatureDynamic Componentv-if/v-else Rendering
Syntax<component :is="..." /><div v-if="...">...</div>
Reusabilityβœ… High❌ Limited to static markup
State Preservationβœ… (with <keep-alive>)❌ No
Use CasesTabs, forms, modalsSimple toggling or one-time

🚫 Common Mistakes

❌ Mistakeβœ… Solution
Using undefined component namesAlways register components properly
Forgetting to wrap with <keep-alive>Wrap only if you want to preserve state
Hardcoding logic in the templateUse 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 :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

Vue Dynamic Components

Or Copy Link

CONTENTS
Scroll to Top