← Back to Vue.js Course | Chapter 4: Components | Lesson 6 of 7

Dynamic Components का परिचय

is attribute वाला component tag runtime पर यह बदलता है कि कौन-सा component display हो।

In this page:

  1. Dynamic Components
Syntax
markup
<component :is="currentComponent"></component>

Dynamic Components

:is को एक component name या definition से bind करें और Vue वहाँ वह component render करता है। यह tabs और wizards के लिए ideal है। switch करते वक्त हर component की state preserve करने के लिए इसे keep-alive में wrap करें। ध्यान दें कि native elements पर is एक अलग form इस्तेमाल करता है।

Note: component :is को keep-alive से combine करें ताकि tabs अपनी state याद रखें।

उदाहरण: Dynamic components

markup
<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
</head>
<body>
<div id="app">
  <button @click="current = 'tab-a'">A</button>
  <button @click="current = 'tab-b'">B</button>
  <component :is="current"></component>
</div>
<script>
  const app = Vue.createApp({ data() { return { current: "tab-a" }; } });
  app.component("tab-a", { template: "<p>This is tab A</p>" });
  app.component("tab-b", { template: "<p>This is tab B</p>" });
  app.mount("#app");
  document.querySelectorAll("button")[1].click();
  Vue.nextTick(() => console.log("now showing:", document.querySelector("p").textContent));
</script>
</body>
</html>

<!-- Output:
Rendered: ABThis is tab B
console: now showing: This is tab B
-->
Live Example
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. एक unregistered component के लिए एक string pass करना
  2. keep-alive के बिना switch करते वक्त state खोना
  3. Component objects को wrap करने वाले Reactive proxies
चैप्टर सारांश
  • <component :is="name"> views बदलता है
  • Tabs के लिए great
  • keep-alive state preserve करता है
  • Components registered होने चाहिए
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.