Performance: Dynamic components with is attribute
This affects rendering speed and responsiveness by controlling which component Vue renders dynamically.
<keep-alive> <component :is="currentComponent" /> </keep-alive> // Keeps inactive components cached to avoid full reloads
<component :is="currentComponent" /> // currentComponent changes frequently triggering full component reloads
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Dynamic component switch without caching | Destroys and recreates full component subtree | 1 reflow per switch | High paint cost due to full redraw | [X] Bad |
| Dynamic component switch with <keep-alive> | Reuses cached DOM nodes, minimal DOM changes | No reflows on cached switches | Low paint cost, only updates needed | [OK] Good |
is attribute do in Vue's <component> tag?<component> tag<component> tag in Vue is a special tag used to render different components dynamically.is attributeis attribute specifies which component to render by binding to a variable or string representing the component name.is sets stylesis with prop bindingis triggers lifecycle hooksMyButton in Vue?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.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.<template>
<component :is="currentView" />
</template>
<script setup>
import HomeView from './HomeView.vue'
import AboutView from './AboutView.vue'
const currentView = ref('HomeView')
</script>currentViewcurrentView is a ref holding the string 'HomeView'.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.<template> <component :is="currentComp" /> </template> <script setup> import Foo from './Foo.vue' const currentComp = Foo </script>
currentCompcurrentComp is assigned the imported component object Foo, which is valid for dynamic components.component tag usage<component> tag uses :is="currentComp" correctly, and self-closing syntax is allowed in Vue templates.HomeView, ProfileView, and SettingsView dynamically based on a button click. Which approach correctly implements this using Vue's dynamic components?currentView must hold the component object, not a string, unless the string matches a registered component name.<template> <component :is="currentView" /> <button @click="currentView = 'HomeView'">Home</button> <button @click="currentView = 'ProfileView'">Profile</button> <button @click="currentView = 'SettingsView'">Settings</button> </template> <script setup> import HomeView from './HomeView.vue' import ProfileView from './ProfileView.vue' import SettingsView from './SettingsView.vue' const currentView = ref(HomeView) </script>initializes
currentView with a component object but sets it to strings on button clicks, causing mismatch. <template> <component :is="currentView" /> <button @click="currentView = HomeView">Home</button> <button @click="currentView = ProfileView">Profile</button> <button @click="currentView = SettingsView">Settings</button> </template> <script setup> import HomeView from './HomeView.vue' import ProfileView from './ProfileView.vue' import SettingsView from './SettingsView.vue' const currentView = ref(HomeView) </script>initializes and updates
currentView with component objects consistently, which is correct. <template>
<component :is="currentView" />
<button @click="currentView = 'HomeView'">Home</button>
<button @click="currentView = 'ProfileView'">Profile</button>
<button @click="currentView = 'SettingsView'">Settings</button>
</template>
<script setup>
import HomeView from './HomeView.vue'
import ProfileView from './ProfileView.vue'
import SettingsView from './SettingsView.vue'
const currentView = ref('HomeView')
</script> uses strings only, but these strings are not registered component names, so Vue won't render them. <template>
<component :is="currentView" />
<button @click="currentView = HomeView">Home</button>
<button @click="currentView = ProfileView">Profile</button>
<button @click="currentView = SettingsView">Settings</button>
</template>
<script setup>
import HomeView from './HomeView.vue'
import ProfileView from './ProfileView.vue'
import SettingsView from './SettingsView.vue'
const currentView = ref('HomeView')
</script> mixes component objects and strings inconsistently, causing errors.Pass the test and this pattern moves unknown