1. 作用: 让不展示的路由组件保持挂载,不被销毁
2. 具体编码
//News.vue
<template>
<ul>
<li>news001 <input type="text"/></li>
<li>news002 <input type="text"/></li>
<li>news003 <input type="text"/></li>
</ul>
</template>
<script>
export default {
name: 'News',
beforeDestroy(){
console.log('News组件即将被销毁了')
}
}
</script>
//Home.vue
<template>
<div>
<h2>home组件内容</h2>
<div>
<ul class="nav nav-tabs">
<li>
<router-link
class="list-group-item"
active-class="active"
to="/home/news">
News
</router-link>
</li>
<li>
<router-link
class="list-group-item"
active-class="active"
to="/home/message">
message
</router-link>
</li>
</ul>
//include值必须是组件名!!!(让哪个组件不被销毁就写哪个组件名)
//同时须清楚知道<keep-alive>包裹的是哪个部分,不能直接在News.vue的外层包裹。
//缓存一个路由组件
<keep-alive include="News">
<router-view></router-view>
</keep-alive>
//缓存多个路由组件
<keep-alive :include=['News','Message']>
<router-view></router-view>
</keep-alive>
</div>
</div>
</template>
<script>
export default {
name: 'Home',
}
</script>