> 動態(tài)組件:不同組件之間進行動態(tài)切換,通過 Vue 的 <component> 元素加一個特殊的 is attribute 實現(xiàn) #### 1. 基礎使用 --- `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. 動態(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)