Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
Vue 16- dynamic components and v-once
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script src="./js/vue.js"></script>
</head>
<body>
<div id="root">
<!-- Vue自带的动态组件<component> -->
<!-- 动态组件会根据is属性绑定的数据变化,自动加载不同的组件 -->
<component :is="type"></component>
<!-- 动态组件其他写法 -->
<!-- <child-one v-if="type === 'child-one'"></child-one> -->
<!-- <child-two v-if="type === 'child-two'"></child-two> -->
<button @click="handleBtn">change</button>
</div>
<script>
// 通过v-once指令可以有效提高静态内容的展示效率
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:{
handleBtn: function(){
this.type = (this.type === 'child-one'? 'child-two' : 'child-one');
}
}
})
</script>
</body>
</html>