Vuejs学习笔记(三)-20.keep-alive标签的使用
keep-alive保持活跃,这个如何使用?先看一些例子
一、在组件中使用生命周期回调函数
created(),destoryed(*),activated(),deactivated()
分别是创建,销毁,活跃,不活跃。
Home.vue中定义这4个函数
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <template> 2 <div> 3 <h2>我是首页</h2> 4 <router-link to="/home/news">新闻</router-link> 5 <router-link to="/home/messages">消息</router-link> 6 <router-view></router-view> 7 </div> 8 </template> 9 10 <script> 11 export default { 12 name: "Home", 13 created(){ 14 // document.title = '首页' 15 console.log('created'); 16 }, 17 destroyed(){ 18 console.log('destoryed'); 19 }, 20 activated(){ 21 console.log('activated'); 22 }, 23 deactivated(){ 24 console.log('deactivated'); 25 } 26 } 27 </script> 28 29 <style scoped> 30 31 </style>
此时打开页面,在首页和其他页面之间进行跳转
测试步骤:
1.如果点击首页,就表示Home页面组件已创建,因此有created()调用情况。其他三种没有。
2.切换到首页内的消息和新闻,也不调用任何函数。
3.切换到其他页,如切换到关于后,会调用destoryed(),表示Home组件被销毁。
二、保持组件不被销毁
需求:希望首页点击消息后,切换到别的页面,再切换回来,仍然显示首页-消息组件
这个时候需要用到keep-alive
测试步骤:
1.在App.vue中的<router-view>标签外部使用<keep-alive>标签
代码如下:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <template> 2 <div> 3 <router-link to="/home" replace>首页</router-link> 4 <router-link to="/about" replace>关于</router-link> 5 <router-link :to="'/user/'+userName" replace>用户</router-link> 6 <!-- <router-link :to="{path:'/profile',query:{name:'invoker',age:1000,skillcount:10}}">档案</router-link>--> 7 <button @click="profileClick">档案</button> 8 <keep-alive> 9 <router-view></router-view> 10 </keep-alive> 11 </div> 12 </template> 13 14 <script> 15 16 export default { 17 name: 'App', 18 data(){ 19 return{ 20 userName:'invoker' 21 } 22 }, 23 methods:{ 24 profileClick(){ 25 this.$router.replace({ 26 path:'/profile', 27 query:{ 28 name:'invoker', 29 age:1000, 30 skillcount:10 31 } 32 }) 33 } 34 } 35 36 } 37 </script> 38 39 <style> 40 41 </style>
2.此时再来看前端Home组件在切换时是否destroyed(),应该是不会销毁的,被保持了。
由此得出结果,keep-alive在App.vue中保持了所有组件的活跃状态。不会重复创建新的组件。
为了完成这个需求,需要使用到组件内部守卫 beforeRouteLeave(to,from,next)
Home.vue组件中
1.在activated()函数中将当前路由地址传给路由路径栈中。
2.在离开当前组件时,组件守卫 beforeRouteLeave(to,from,next)中奖当前组件付给该组件当前路径。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <template> 2 <div> 3 <h2>我是首页</h2> 4 <router-link to="/home/news">新闻</router-link> 5 <router-link to="/home/messages">消息</router-link> 6 <router-view></router-view> 7 </div> 8 </template> 9 10 <script> 11 export default { 12 name: "Home", 13 created(){ 14 // document.title = '首页' 15 console.log('created'); 16 }, 17 destroyed(){ 18 console.log('destoryed'); 19 }, 20 activated(){ 21 console.log('activated'); 22 this.$router.push(this.path) 23 }, 24 deactivated(){ 25 console.log('deactivated'); 26 }, 27 beforeRouteLeave(to,from,next){ 28 this.path = this.$route.path 29 next() 30 } 31 } 32 </script> 33 34 <style scoped> 35 36 </style>
3.修改router/index.js中的默认路径
代码如下:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 import Vue from 'vue' 2 import VueRouter from 'vue-router' 3 4 // 路由的懒加载 5 const Home = () => import('../components/Home') 6 const About = () => import('../components/About') 7 const User = () => import('../components/User') 8 const HomeMessages = ()=>import('../components/HomeMessages') 9 const HomeNews = ()=>import('../components/HomeNews') 10 const Profile = ()=>import('../components/Profile') 11 12 Vue.use(VueRouter) 13 14 const routes = [ 15 // { 16 // path: '', 17 // redirect: '/home' 18 // }, 19 { 20 path: '/home', 21 component: Home, 22 meta:{ 23 title:'首页' 24 }, 25 children:[ 26 { 27 path:'', 28 redirect:'news' 29 }, 30 { 31 path:'news', 32 component:HomeNews 33 }, 34 { 35 path:'messages', 36 component:HomeMessages 37 } 38 ] 39 }, 40 { 41 path: '/about', 42 component: About, 43 meta:{ 44 title:'关于' 45 } 46 }, 47 { 48 path: '/user/:userName1', 49 component: User, 50 meta:{ 51 title:'用户' 52 } 53 }, 54 { 55 path:'/profile', 56 component: Profile, 57 meta:{ 58 title:'档案' 59 } 60 } 61 ] 62 63 const router = new VueRouter({ 64 routes, 65 mode:'history' 66 }) 67 //全局前置守卫 68 router.beforeEach((to,from,next)=>{ 69 document.title = to.matched[0].meta.title 70 next() 71 }) 72 //全局后置钩子 73 router.afterEach((to,from)=>{ 74 // 没有next 75 // console.log(to); 76 // console.log(from); 77 }) 78 79 export default router
页面效果,离开首页新闻或则首页消息后,再切回来时,仍然显示离开首页的地址。
三、可以选择哪些组件保持活跃,哪些组件不保持活跃,使用keep-alive的属性 include,exclude.
1.所有组件添加生命周期回调函数。
about组件
home组件:
Profile组件:
User组件:
需求:若希望部分组件保持活跃,部分组件不保持活跃,需要在App.vue文件中修改keep-alive的属性
这样设置后,Profile每次点击,都会销毁,而其他组件每次点击,不会被销毁。
本文来自博客园,作者:kaer_invoker,转载请注明原文链接:https://www.cnblogs.com/invoker2021/p/14988714.html