Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

Vue Tutorial

Vue HOME Vue Intro Vue Directives Vue v-bind Vue v-if Vue v-show Vue v-for Vue Events Vue v-on Vue Methods Vue Event Modifiers Vue Forms Vue v-model Vue CSS Binding Vue Computed Properties Vue Watchers Vue Templates

Scaling Up

Vue Why, How and Setup Vue First SFC Page Vue Components Vue Props Vue v-for Components Vue $emit() Vue Fallthrough Attributes Vue Scoped Styling Vue Local Components Vue Slots Vue v-slot Vue Scoped Slots Vue Dynamic Components Vue Teleport Vue HTTP Request Vue Template Refs Vue Lifecycle Hooks Vue Provide/Inject Vue Routing Vue Form Inputs Vue Animations Vue Animations with v-for Vue Build Vue Composition API

Vue Reference

Vue Built-in Attributes Vue Built-in Components Vue Built-in Elements Vue Component Instance Vue Directives Vue Instance Options Vue Lifecycle Hooks

Vue Examples

Vue Examples Vue Exercises Vue Quiz Vue Syllabus Vue Study Plan Vue Server Vue Certificate

Vue <component> Element


Example

Using the built-in <component> element with the is attribute to create a dynamic component.

<template>
  <h1>Dynamic Components</h1>
  <p>App.vue switches between which component to show.</p>
  <button @click="toggleValue = !toggleValue">Switch component</button>
  <component :is="activeComp"></component>
</template>
Run Example »

See more examples below.


Definition and Usage

The built-in <component> element is used together with the built-in is attribute to create an HTML element, or a Vue component.

HTML element: To create an HTML element with the <component> element, the is attribute is set equal to the name of the HTML element we want to create, either directly (Example 1), or dynamically by the use of v-bind (Example 2).

Vue component: To render a Vue component with the <component> element, the is attribute is set equal to the name of the Vue component we want to create, either directly (Example 3), or dynamically by the use of v-bind to create a dynamic component (Example 4).

When creating a dynamic component, the built-in <KeepAlive> component can be used around the <component> element to remember the state of components that are not active. (Example 5)

The active component in a dynamic component can also be changed by using a ternary expression with the is attribute. (Example 6)

Note: The v-model directive does not work with native HTML form input tags (such as <input> or <option>) created with the <component> element. (Example 7)


Props

Prop Description
is Required. Is set equal to the component that should be active, or is set equal to the HTML element to be created.

More examples

Example 1

Using the built-in <component> element to create a <div> element.

<template>
  <h2>Example Built-in 'component' Element</h2>
  <p>The component element is rendered as a div element with is="div":</p>
  <component is="div">This is a DIV element</component>
</template>

<style scoped>
div {
  border: solid black 1px;
  background-color: lightgreen;
}
</style>
Run Example »

Example 2

Using the built-in <component> element to toggle between an ordered list and an unordered list.

<template>
  <h2>Example Built-in 'component' Element</h2>
  <p>Using the component element to toggle between an ordered list (ol), and an unordered list (ul):</p>
  <button v-on:click="toggleValue = !toggleValue">Toggle</button>
  <p>Animals from around the world</p>
  <component :is="tagType">
    <li>Kiwi</li>
    <li>Jaguar</li>
    <li>Bison</li>
    <li>Snow Leopard</li>
  </component>
</template>

<script>
export default {
  data() {
    return {
      toggleValue: true
    }
  },
  computed: {
    tagType() {
      if (this.toggleValue) {
        return 'ol'
      }
      else {
        return 'ul'
      }
    }
  }
}
</script>
Run Example »

Example 3

Using the built-in <component> element to render a component by providing the name of the component to the is attribute.

App.vue:

<template>
  <h2>Example Built-in 'is' Attribute</h2>
  <p>The component element below is set to be a component by the use of 'is="child-comp"'.</p>
  <component is="child-comp"></component>
</template>

ChildComp.vue:

<template>
  <div>
    <h3>ChildComp.vue</h3>
    <p>This is the child component</p>
  </div>
</template>

<style scoped>
div {
  border: solid black 1px;
  background-color: lightgreen;
  padding: 10px;
  max-width: 250px;
  margin-top: 20px;
}
</style>
Run Example »

Example 4

Using the built-in <component> element to create a dynamic component, where we can switch between two components.

<template>
  <h1>Dynamic Components</h1>
  <p>App.vue switches between which component to show.</p>
  <button @click="toggleValue = !toggleValue">Switch component</button>
  <component :is="activeComp"></component>
</template>

<script>
  export default {
    data () {
      return {
        toggleValue: true
      }
    },
    computed: {
      activeComp() {
        if(this.toggleValue) {
          return 'comp-one'
        }
        else {
          return 'comp-two'
        }
      }
    }
  }
</script>

<style>
  #app {
    width: 350px;
    margin: 10px;
  }
  #app > div {
    border: solid black 2px;
    padding: 10px;
    margin-top: 10px;
  }
</style>
Run Example »

Example 5

The built-in <KeepAlive> component is used around the <component> element to remember the inputs when the components are switched between.

<template>
  <h1>Dynamic Components</h1>
  <p>App.vue switches between which component to show.</p>
  <p>With the <KeepAlive> tag the components now remember the user inputs.</p>
  <button @click="toggleValue = !toggleValue">Switch component</button>
  <KeepAlive>
    <component :is="activeComp"></component>
  </KeepAlive>
</template>

<script>
  export default {
    data () {
      return {
        toggleValue: true
      }
    },
    computed: {
      activeComp() {
        if(this.toggleValue) {
          return 'comp-one'
        }
        else {
          return 'comp-two'
        }
      }
    }
  }
</script>

<style>
  #app {
    width: 350px;
    margin: 10px;
  }
  #app > div {
    border: solid black 2px;
    padding: 10px;
    margin-top: 10px;
  }
  h2 {
    text-decoration: underline;
  }
</style>
Run Example »

Example 6

Using the <component> element with the is attribute and a ternary expression to toggle which component should be active.

<template>
  <h1>Dynamic Components</h1>
  <p>Refresh the page and there is a chance the dynamic component will toggle.</p>
  <component :is="Math.random() > 0.5 ? 'comp-one' : 'comp-two'"></component>
</template>

<style>
  #app {
    width: 350px;
    margin: 10px;
  }
  #app > div {
    border: solid black 2px;
    padding: 10px;
    margin-top: 10px;
  }
</style>
Run Example »

Example 7

Demonstrating that the v-model directive does not work with <input> elements created using the <component> element.

<template>
  <h1>Dynamic Components</h1>
  <p><mark>The v-model directive does not work with input element created with the component element.</mark></p>
  <hr>
  <p>Does not work, not updating:</p>
  <component is="input" type="number" v-model="inpVal1"></component> (try to change value)
  <p class="pResult1">inpVal1: {{ inpVal1 }}</p>
  <hr>
  <p>How it should work, updates:</p>
  <input type="number" v-model="inpVal2"> (try to change value)
  <p class="pResult2">inpVal2: {{ inpVal2 }}</p>
</template>

<script>
export default {
  data() {
    return {
      inpVal1: 4,
      inpVal2: 7,
    }
  }
}
</script>

<style>
#app {
  width: 350px;
  margin: 10px;
}
.pResult1 {
  background-color: lightpink;
  font-family: 'Courier New', Courier, monospace;
  font-weight: bold;
  padding: 5px;
}
.pResult2 {
  background-color: lightgreen;
  font-family: 'Courier New', Courier, monospace;
  font-weight: bold;
  padding: 5px;
}
</style>
Run Example »

Related Pages

Vue Tutorial: Vue Components

Vue Tutorial: Dynamic Components

Vue Tutorial: Vue Form Inputs

Vue Tutorial: Vue v-model Directive

Vue Reference: Vue is Attribute

Vue Reference: Vue v-bind Directive

Vue Reference: Vue v-model Directive


×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2024 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.