> 動(dòng)態(tài)組件:不同組件之間進(jìn)行動(dòng)態(tài)切換,通過 Vue 的 <component> 元素加一個(gè)特殊的 is attribute 實(shí)現(xiàn) #### 1. 基礎(chǔ)使用 --- `component` 的 `is 屬性值是組件名`,就可以調(diào)用該組件 ``` <component is="comb"></component> ``` ```html <div id="app"> <component is="comb"></component> </div> <script> var coma = { template: '<div>aaaa</div>' } var comb = { template: '<div>bbbb</div>' } let vm = new Vue({ el: '#app', components: { coma: coma, comb: comb, } }) </script> ``` #### 2. 動(dòng)態(tài)調(diào)用組件示例 --- ```html <div id="app"> <button @click="changeComponent('coma')">coma</button> <button @click="changeComponent('comb')">comb</button> <button @click="changeComponent('comc')">comc</button> <component :is="com_name"></component> </div> <script> var coma = { template: '<div>aaaa</div>' } var comb = { template: '<div>bbbb</div>' } var comc = { template: '<div>cccc</div>' } let vm = new Vue({ el: '#app', data: { com_name: 'coma' }, components: { coma: coma, comb: comb, comc: comc, }, methods: { changeComponent: function(name) { this.com_name = name } } }) </script> ``` 代碼效果 ![](https://img.itqaq.com/art/content/263af4c193f1b48ddcdc13df01744024.png)