Vue.js 提供了一个特殊的元素 <component> 用来动态地挂载不同的组件,使用 js 特性来选择要挂载的组件.示例代码如下: <template> <div class="wrap"> <component :is="active"></component> <button @click="switchTap('temp')">切换到temp</button> <button @click="switchTap('tempA')">切换到tempA</button> </div> </template> <script> import Temp from '../components/temp' import TempA from '../components/tempA' export default { name: 'practice', components: { Temp, TempA }, data () { return { books: [ { name: 'aaa'}, { name: 'bbb'}, { name: 'ccc'} ], colors: [ { color: 'red'}, { color: 'blue'}, { color: 'yellow'} ], active: 'Temp' } }, methods: { switchTap(val) { this.active = val } } } </script> <style scoped> *{ list-style: none; } </style>
动态地改变 active的值就可以动态挂载了.