Vue <component> 元素
实例
使用具有 is 属性的内置 is 元素来创建动态组件。
<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>
定义与用法
内置的 <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> 元素。
<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>
实例 2
使用内置的 <component> 元素在有序列表和无序列表之间切换。
<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>
实例 3
使用内置的 <component> 元素,通过向 is 属性提供组件的名称来渲染组件。
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>
实例 4
使用内置的 <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><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>
实例 5
内置的 <KeepAlive> 组件围绕 <component> 元素使用,以在组件之间切换时记忆输入。
<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>
实例 6
使用带有 is 属性和三元表达式的 <component> 元素来切换哪个组件应该处于活动状态。
<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>
实例 7
演示 v-model 指令不适用于使用 <component> 元素创建的 <input> 元素。
<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>