A dynamic render pattern in Vue means showing or hiding parts of the page based on data or user actions, so the page changes without reloading.
Click to reveal answer
beginner
How does the v-if directive work in Vue?
v-if shows or hides an element depending on a condition. If the condition is true, the element appears; if false, it disappears from the page.
Click to reveal answer
intermediate
What is the difference between v-if and v-show?
v-if adds or removes elements from the page, while v-show only hides or shows them using CSS. v-show is faster to toggle but v-if saves resources when elements are rarely shown.
Click to reveal answer
intermediate
How can you render different components dynamically in Vue?
Use the <component :is="componentName" /> tag, where componentName is a variable holding the component to show. This lets Vue switch components on the fly.
Click to reveal answer
beginner
Why is key important when rendering lists dynamically in Vue?
The key helps Vue track each item uniquely. It improves performance and avoids bugs by telling Vue which items changed, moved, or stayed the same.
Click to reveal answer
Which Vue directive removes an element from the DOM when a condition is false?
Av-if
Bv-show
Cv-for
Dv-bind
✗ Incorrect
v-if adds or removes elements from the DOM based on the condition.
What does the v-show directive do when its condition is false?
AThrows an error
BRemoves the element from the DOM
CHides the element using CSS
DRe-renders the entire component
✗ Incorrect
v-show hides the element by setting CSS display: none.
The <component :is="componentName" /> tag lets Vue render different components dynamically.
Why should you use a key when rendering lists?
ATo style the list items
BTo help Vue track items for better updates
CTo add event listeners
DTo sort the list automatically
✗ Incorrect
The key helps Vue identify each item uniquely for efficient updates.
Which directive is best when you want to toggle visibility very often without removing elements?
Av-show
Bv-if
Cv-for
Dv-on
✗ Incorrect
v-show toggles visibility with CSS, making it faster for frequent changes.
Explain how Vue's v-if and v-show differ and when to use each.
Think about performance and how often you change visibility.
You got /4 concepts.
Describe how to render different components dynamically in Vue and why this is useful.
Imagine changing tabs or pages inside one area.
You got /4 concepts.
Practice
(1/5)
1. What does the :is directive do in Vue's dynamic render patterns?
easy
A. It dynamically switches which component or element to render.
B. It binds a CSS class to an element.
C. It listens for user input events.
D. It creates a loop to render multiple items.
Solution
Step 1: Understand the purpose of :is
The :is directive in Vue allows you to dynamically choose which component or HTML element to render based on a variable.
Step 2: Compare with other directives
Unlike class binding or event listening, :is changes the actual rendered tag or component, making the UI flexible.
Final Answer:
It dynamically switches which component or element to render. -> Option A
Quick Check:
:is = dynamic component switch [OK]
Hint: Remember :is changes the tag or component shown [OK]
Common Mistakes:
Confusing :is with event handlers
Thinking :is binds CSS classes
Assuming :is creates loops
2. Which of the following is the correct syntax to conditionally render a component in Vue using v-if?
easy
A. <MyComponent :is='show' />
B. <MyComponent v-if='show' />
C. <MyComponent v-for='show' />
D. <MyComponent v-show='show' />
Solution
Step 1: Identify the directive for conditional rendering
v-if is used to conditionally render elements or components based on a boolean expression.
Step 2: Check the syntax correctness
The syntax <MyComponent v-if='show' /> correctly applies v-if to render MyComponent only if show is true.
Final Answer:
<MyComponent v-if='show' /> -> Option B
Quick Check:
v-if = conditional render [OK]
Hint: Use v-if to show/hide components conditionally [OK]
Common Mistakes:
Confusing v-if with v-show
Using v-for for conditional rendering
Misusing :is for boolean conditions
3. Given the Vue template:
<component :is="currentView" />
and the data:
data() { return { currentView: 'HomeView' } }
What will be rendered if HomeView is a registered component?
medium
A. An error will occur because component tag is invalid.
B. Nothing will render because currentView is a string.
C. The literal text 'HomeView' will be shown.
D. The HomeView component will be rendered.
Solution
Step 1: Understand <component :is=... /> usage
The <component> tag with :is dynamically renders the component named by the string in currentView.
Step 2: Check if HomeView is registered
Since HomeView is registered, Vue will render that component in place of <component>.
Final Answer:
The HomeView component will be rendered. -> Option D
Quick Check:
Dynamic component name string renders component [OK]
Hint: Dynamic :is renders component named by string [OK]
Common Mistakes:
Thinking string names don't render components
Confusing component tag as invalid HTML
Expecting literal text instead of component
4. What is wrong with this Vue template snippet?
<component :is="viewName" v-if="viewName" />
where viewName can be null or a component name string.
medium
A. It will throw an error if viewName is null.
B. The :is binding must be a component object, not a string.
C. There is no problem; it works correctly.
D. The v-if should be replaced with v-show.
Solution
Step 1: Analyze v-if="viewName" behavior
The v-if prevents rendering when viewName is null or falsy, so the component won't render or cause errors.
Step 2: Check :is binding type
:is accepts a string naming a registered component or a component object, so a string is valid.
Final Answer:
There is no problem; it works correctly. -> Option C
Quick Check:
v-if blocks null, :is accepts string [OK]
Hint: v-if prevents null rendering errors [OK]
Common Mistakes:
Assuming null causes errors without v-if
Confusing v-show with v-if
Thinking :is only accepts objects
5. You want to render a list of components dynamically based on an array of component names, but only if the component name is not empty. Which Vue pattern correctly achieves this?
hard
A.
<template>
<component
v-for="name in componentNames"
:key="name"
:is="name"
v-if="name"
/>
</template>
B.
<template>
<component
v-for="name in componentNames"
:key="name"
:is="name"
/>
</template>