1.引用模板
将组件内容放到模板
<template id="">
组件内容
</template>
中并引用,使用id标记这个模板为哪一个组件的内容。
template标签中只能有一个根部元素
<template id="luna">
<h3>我是hello组件</h3>
</template>
var vm=new Vue({
el:'#itany',
data:{
flag:'my-hello'
},
components:{
'my-hello':{
template:'#luna',
}
},
}
});
2.动态组件
<component :is="">组件
多个组件使用同一个挂载点,然后动态的在它们之间切换
:is后面的参数为需要显示的组件的名字
<keep-alive>组件
使用keep-alive组件缓存非活动组件,可以保留状态,避免重新渲染,默认每次都会销毁非活动组件并重新创建
<keep-alive>
<component :is="flag"></component>
</keep-alive>
