<body>
<div id="root">
<!-- 动态组件 -->
<!-- <component :is="type"></component> -->
<child-one v-if="type === 'child-one'"></child-one>
<child-two v-if="type === 'child-two'"></child-two>
<button @click="handleClick">change</button>
</div>
<script>
Vue.component('child-one',{
template: "<div v-once>child-one</div>"
})
Vue.component('child-two',{
template: "<div v-once>child-two</div>"
})
var vm = new Vue({
el: '#root',
data: {
type: 'child-one'
},
methods: {
handleClick: function() {
this.type = (this.type === 'child-one' ? 'child-two':'child-one')
}
}
})
// 注:v-once 可以提高静态页面的展示效率
</script>
</body>