Dynamic components let you switch which component shows on the page without reloading. This helps make your app feel smooth and fast.
Showing different views based on user choices, like tabs or menus.
Loading different forms or content blocks without changing the page.
Building a dashboard where widgets can change dynamically.
Creating a step-by-step wizard where each step is a different component.
Syntax
Vue
<component :is="componentName"></component>
The tag is a special Vue element that renders the component named in the 'is' attribute.
You can bind 'is' to a variable to change the component dynamically.
Examples
Renders the component stored in the currentView variable.
Vue
<component :is="currentView"></component>
Renders the MyButton component directly.
Vue
<component is="MyButton"></component>
Switches the displayed component based on selectedComponent reactive data.
Vue
<component :is="selectedComponent"></component>
Sample Program
This Vue component shows two buttons. Clicking a button changes which component is shown below. The component tag with :is switches between HelloWorld and GoodbyeWorld components dynamically.
Make sure the components you want to switch between are registered in your parent component.
Dynamic components keep their own state unless you force them to reset.
You can also use async components with is for lazy loading.
Summary
Dynamic components let you swap views without reloading the page.
Use the component tag with the is attribute bound to a variable.
This makes your app interactive and flexible for many UI scenarios.
Practice
(1/5)
1. What does the is attribute do in Vue's <component> tag?
easy
A. It binds data to the component's props.
B. It sets the CSS style of the component.
C. It tells Vue which component to render dynamically.
D. It triggers a lifecycle hook in the component.
Solution
Step 1: Understand the role of <component> tag
The <component> tag in Vue is a special tag used to render different components dynamically.
Step 2: Identify the purpose of the is attribute
The is attribute specifies which component to render by binding to a variable or string representing the component name.
Final Answer:
It tells Vue which component to render dynamically. -> Option C
Quick Check:
Dynamic component selection = is attribute [OK]
Hint: Remember: picks component x [OK]
Common Mistakes:
Thinking is sets styles
Confusing is with prop binding
Assuming is triggers lifecycle hooks
2. Which of the following is the correct syntax to use a dynamic component named MyButton in Vue?
easy
A.
B.
C.
D.
Solution
Step 1: Understand binding syntax for dynamic components
The is attribute must be bound with : to a variable or string. To specify a component by name as a string, use quotes inside the binding.
Step 2: Check each option's syntax
uses is="MyButton" without binding, so Vue treats it as a literal tag, not a component name. binds MyButton as a variable, but if MyButton is not defined in the script, it fails. correctly binds is to the string 'MyButton'. uses is='MyButton' without binding, which is invalid for dynamic components.
Final Answer:
<component :is='"MyButton"'></component> -> Option A
Quick Check:
Use :is with quotes for string component names [OK]
Hint: Use :is with quotes for string names, no quotes without : [OK]
Common Mistakes:
Omitting the colon for binding
Using quotes without binding
Binding to undefined variables
3. Given the Vue template:
<template>
<component :is="currentView" />
</template>
<script setup>
import HomeView from './HomeView.vue'
import AboutView from './AboutView.vue'
const currentView = ref('HomeView')
</script>
What will be rendered initially?
medium
A. The HomeView component
B. The AboutView component
C. A runtime error because currentView is a string
D. An empty component (nothing rendered)
Solution
Step 1: Analyze the value of currentView
The variable currentView is a ref holding the string 'HomeView'.
Step 2: Understand how Vue resolves dynamic components
Vue expects the is attribute to be a component object or a registered component name. Here, 'HomeView' is a string but the component is imported as HomeView object, not registered globally or locally by that string name.
Step 3: Result of passing string without registration
Since 'HomeView' is not registered as a component name, Vue renders nothing (empty component) without error.
Final Answer:
An empty component (nothing rendered) -> Option D
Quick Check:
Unregistered string name = empty render [OK]
Hint: Dynamic component string must be registered or component object [OK]
Common Mistakes:
Assuming string name auto-resolves to imported component
B. The currentComp variable should be a string, not a component object
C. Missing quotes around component name in is attribute
D. The component tag must be self-closing with a closing slash
Solution
Step 1: Check the type of currentComp
currentComp is assigned the imported component object Foo, which is valid for dynamic components.
Step 2: Verify the component tag usage
The <component> tag uses :is="currentComp" correctly, and self-closing syntax is allowed in Vue templates.
Final Answer:
No error; this code works correctly -> Option A
Quick Check:
Passing component object to :is works fine [OK]
Hint: Pass component objects directly to :is for dynamic rendering [OK]
Common Mistakes:
Thinking :is must always be a string
Confusing self-closing tags as errors
Expecting quotes around component objects
5. You want to switch between three components HomeView, ProfileView, and SettingsView dynamically based on a button click. Which approach correctly implements this using Vue's dynamic components?