Vue <component> 元素

实例

使用具有 is 属性的内置 is 元素来创建动态组件。

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

定义与用法

内置的 <component> 元素与内置的 is 属性一起用于创建 HTML 元素或 Vue 组件。

HTML 元素:要使用 <component> 元素创建 HTML 元素,将 is 属性设置为等于我们要创建的 HTML 元素的名称,可以直接创建(实例1),也可以使用 v-bind 动态创建(实例2)。

Vue 组件:要使用 <component> 元素渲染 Vue 组件,is 属性设置为等于我们要创建的 Vue 组件的名称,可以直接创建(实例3),也可以使用 v-bind 动态创建动态组件(实例4)。

创建动态组件时,可以在 <component> 元素周围使用内置的 <KeepAlive> 组件来记住未激活组件的状态。(实例5)

动态组件中的活动组件也可以通过使用具有 is 属性的三元表达式来更改。(实例6)

注意v-model 指令不适用于使用 <component> 元素创建的本地 HTML 表单输入标签(如 <input> 或 <option>)。(实例7)

Props

Prop描述
is必填。 Is 设置为等于应该活动的组件,或者设置为等于要创建的 HTML 元素

更多实例

实例 1

使用内置的 <component> 元素来创建 <div> 元素。

  1. <template>
  2. <h2>Example Built-in 'component' Element</h2>
  3. <p>The component element is rendered as a div element with is="div":</p>
  4. <component is="div">This is a DIV element</component>
  5. </template>
  6. <style scoped>
  7. div {
  8. border: solid black 1px;
  9. background-color: lightgreen;
  10. }
  11. </style>
实例 2

使用内置的 <component> 元素在有序列表和无序列表之间切换。

  1. <template>
  2. <h2>Example Built-in 'component' Element</h2>
  3. <p>Using the component element to toggle between an ordered list (ol), and an unordered list (ul):</p>
  4. <button v-on:click="toggleValue = !toggleValue">Toggle</button>
  5. <p>Animals from around the world</p>
  6. <component :is="tagType">
  7. <li>Kiwi</li>
  8. <li>Jaguar</li>
  9. <li>Bison</li>
  10. <li>Snow Leopard</li>
  11. </component>
  12. </template>
  13. <script>
  14. export default {
  15. data() {
  16. return {
  17. toggleValue: true
  18. }
  19. },
  20. computed: {
  21. tagType() {
  22. if (this.toggleValue) {
  23. return 'ol'
  24. }
  25. else {
  26. return 'ul'
  27. }
  28. }
  29. }
  30. }
  31. </script>
实例 3

使用内置的 <component> 元素,通过向 is 属性提供组件的名称来渲染组件。

App.vue:

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

ChildComp.vue:

  1. <template>
  2. <div>
  3. <h3>ChildComp.vue</h3>
  4. <p>This is the child component</p>
  5. </div>
  6. </template>
  7. <style scoped>
  8. div {
  9. border: solid black 1px;
  10. background-color: lightgreen;
  11. padding: 10px;
  12. max-width: 250px;
  13. margin-top: 20px;
  14. }
  15. </style>
实例 4

使用内置的 <component> 元素创建一个动态组件,在其中我们可以在两个组件之间切换。

  1. <template>
  2. <h1>Dynamic Components</h1>
  3. <p>App.vue switches between which component to show.</p>
  4. <button @click="toggleValue = !toggleValue">Switch component</button>
  5. <component :is="activeComp"></component>
  6. </template>
  7. <script>
  8. export default {
  9. data () {
  10. return {
  11. toggleValue: true
  12. }
  13. },
  14. computed: {
  15. activeComp() {
  16. if(this.toggleValue) {
  17. return 'comp-one'
  18. }
  19. else {
  20. return 'comp-two'
  21. }
  22. }
  23. }
  24. }
  25. </script>
  26. <style>
  27. #app {
  28. width: 350px;
  29. margin: 10px;
  30. }
  31. #app > div {
  32. border: solid black 2px;
  33. padding: 10px;
  34. margin-top: 10px;
  35. }
  36. </style>
实例 5

内置的 <KeepAlive> 组件围绕 <component> 元素使用,以在组件之间切换时记忆输入。

  1. <template>
  2. <h1>Dynamic Components</h1>
  3. <p>App.vue switches between which component to show.</p>
  4. <p>With the <KeepAlive> tag the components now remember the user inputs.</p>
  5. <button @click="toggleValue = !toggleValue">Switch component</button>
  6. <KeepAlive>
  7. <component :is="activeComp"></component>
  8. </KeepAlive>
  9. </template>
  10. <script>
  11. export default {
  12. data () {
  13. return {
  14. toggleValue: true
  15. }
  16. },
  17. computed: {
  18. activeComp() {
  19. if(this.toggleValue) {
  20. return 'comp-one'
  21. }
  22. else {
  23. return 'comp-two'
  24. }
  25. }
  26. }
  27. }
  28. </script>
  29. <style>
  30. #app {
  31. width: 350px;
  32. margin: 10px;
  33. }
  34. #app > div {
  35. border: solid black 2px;
  36. padding: 10px;
  37. margin-top: 10px;
  38. }
  39. h2 {
  40. text-decoration: underline;
  41. }
  42. </style>
实例 6

使用带有 is 属性和三元表达式的 <component> 元素来切换哪个组件应该处于活动状态。

  1. <template>
  2. <h1>Dynamic Components</h1>
  3. <p>Refresh the page and there is a chance the dynamic component will toggle.</p>
  4. <component :is="Math.random() > 0.5 ? 'comp-one' : 'comp-two'"></component>
  5. </template>
  6. <style>
  7. #app {
  8. width: 350px;
  9. margin: 10px;
  10. }
  11. #app > div {
  12. border: solid black 2px;
  13. padding: 10px;
  14. margin-top: 10px;
  15. }
  16. </style>
实例 7

演示 v-model 指令不适用于使用 <component> 元素创建的 <input> 元素。

  1. <template>
  2. <h1>Dynamic Components</h1>
  3. <p><mark>The v-model directive does not work with input element created with the component element.</mark></p>
  4. <hr>
  5. <p>Does not work, not updating:</p>
  6. <component is="input" type="number" v-model="inpVal1"></component> (try to change value)
  7. <p class="pResult1">inpVal1: {{ inpVal1 }}</p>
  8. <hr>
  9. <p>How it should work, updates:</p>
  10. <input type="number" v-model="inpVal2"> (try to change value)
  11. <p class="pResult2">inpVal2: {{ inpVal2 }}</p>
  12. </template>
  13. <script>
  14. export default {
  15. data() {
  16. return {
  17. inpVal1: 4,
  18. inpVal2: 7,
  19. }
  20. }
  21. }
  22. </script>
  23. <style>
  24. #app {
  25. width: 350px;
  26. margin: 10px;
  27. }
  28. .pResult1 {
  29. background-color: lightpink;
  30. font-family: 'Courier New', Courier, monospace;
  31. font-weight: bold;
  32. padding: 5px;
  33. }
  34. .pResult2 {
  35. background-color: lightgreen;
  36. font-family: 'Courier New', Courier, monospace;
  37. font-weight: bold;
  38. padding: 5px;
  39. }
  40. </style>

分类导航