Given the following Vue 3 component setup, what will be rendered inside the <component :is="currentComp" />?
<template> <component :is="currentComp" /> </template> <script setup> import { ref } from 'vue' const HelloComp = { template: '<p>Hello!</p>' } const ByeComp = { template: '<p>Goodbye!</p>' } const currentComp = ref(HelloComp) </script>
Think about what the is attribute does and how the component name is resolved.
The component tag with :is="currentComp" renders the component referenced by currentComp. Since currentComp holds HelloComp, which is locally defined, Vue renders its template: Hello!.
currentComp after clicking the button?Consider this Vue 3 component:
<template>
<component :is="currentComp" />
<button @click="toggleComp">Toggle</button>
</template>
<script setup>
import { ref } from 'vue'
const currentComp = ref('CompA')
function toggleComp() {
currentComp.value = currentComp.value === 'CompA' ? 'CompB' : 'CompA'
}
</script>Initially, currentComp is 'CompA'. What is its value after the button is clicked twice?
Each click toggles the component name. Think about toggling twice.
The toggleComp function switches currentComp between 'CompA' and 'CompB'. Starting at 'CompA', one click changes it to 'CompB', the second click changes it back to 'CompA'.
is attribute with a dynamic component in Vue 3?Choose the correct syntax to render a dynamic component using the is attribute bound to a variable currentView.
Remember how to bind attributes dynamically in Vue templates.
In Vue, to bind an attribute dynamically, you use : (shorthand) or v-bind:. Option D uses the preferred shorthand :is="currentView". Option D treats is as a static string, not dynamic. Option D uses full v-bind:is="currentView", which works but is more verbose. Option D uses single quotes for the expression, which is valid but Vue style guide prefers double quotes for attribute values.
Given this Vue 3 code snippet:
<template>
<component :is="currentComp" />
</template>
<script setup>
import { ref } from 'vue'
const MyComp = {
template: '<p>Hi there</p>'
}
const currentComp = ref('MyComp')
</script>Why does the component not render and show an error?
Think about how Vue finds components by name.
Vue needs components to be registered either globally or locally to render them by name. Here, MyComp is defined but not registered or exposed, so Vue cannot find it when currentComp is 'MyComp'.
is attribute for dynamic components in Vue?Choose the best explanation for why Vue developers use the is attribute to render dynamic components.
Think about flexibility in UI rendering.
The is attribute lets Vue render different components dynamically based on data or state. This means you can switch which component shows without rewriting the template, making UI flexible and easier to manage.