</keep-alive>
匹配首先检查组件自身的 name 选项,如果 name 选项不可用,则匹配它的局部注册名称(父组件 components 选项的键值)。匿名组件不能被匹配
<keep-alive include="home,archive">
<component :is="currentView"></component>
</keep-alive>上面的代码,表示只缓存home和archive,不缓存posts
<div id="example">
<button @click="change">切换页面</button>
<keep-alive include="home,archive">
<component :is="currentView"></component>
</keep-alive>
</div>
<script src="https://unpkg.com/vue"></script>
<script>
new Vue({
el: '#example',
data:{
index:0,
arr:[
{name:'home',template:`<div>我是主页</div>`},
{name:'posts',template:`<div>我是提交页</div>`},
{name:'archive',template:`<div>我是存档页</div>`}
],
},
computed:{
currentView(){
return this.arr[this.index];
}
},
methods:{
change(){
var len = this.arr.length;
this.index = (++this.index)% len;
},
}
})
</script>总结
以上所述是小编给大家介绍的Vue动态组件实例解析,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!










