【Vue】Re18 Router 第五部分(KeepAlive)
一、KeepAlive概述
默认状态下,用户点击新的路由时,是访问新的组件
那么当前组件是会被销毁的,然后创建新的组件对象出来
如果某些组件频繁的使用,将造成内存空间浪费,也吃内存性能
所以需求是希望组件能被缓存起来,不是立即销毁
生命周期的创建函数 create(); 和销毁函数 destory(); 都将反复调用
二、使用
只需要把router-view做为keep-alive的子元素就行了
<template>
<div id="app">
<router-link to="/home" tag="button" >去首页</router-link>
<router-link to="/about" tag="button" >去关于</router-link>
<router-link :to="/user/+username" tag="button" >用户管理</router-link>
<router-link :to="ppp" tag="button" >profile</router-link>
<!-- <button @click="toProfile" >profile</button>-->
<!-- <button @click="toHome">首页</button>-->
<!-- <button @click="toAbout">关于</button>-->
<keep-alive>
<router-view></router-view>
</keep-alive>
<p>
<button @click="getRouterInstance">获取Router对象</button>
<button @click="getCurrentRouteInstance">获取Route对象</button>
</p>
</div>
</template>
如果组件的周期不再销毁,那么生命状态则发生了改变
在访问时是被激活的状态
如果离开了组件时,则是非激活状态
对应了两个生命周期函数:
activated () {
// todo ...
}
deactivated () {
// todo ...
}
注意!!!上述的函数仅对keep-alive处理的组件有效
三、关于重定向路由BUG问题
/router/index.js
父级路由
/* 重定向首页路由配置 */
{
path : '', /* 缺省值默认指向 '/' */
redirect : '/home',
},
子级路由
children : [ /* 设置子路由 */
{
path : '', /* 这个缺省默认/home */
redirect : 'news',
},
重定向home的时候触发子路由的重定向
向下继续重定向到/home/news
解决方案:
移除子路由的重定向,在组件初始化时重定向一次,后续不再重定向
还有要记录用户之前访问的组件的路由,回到之前的父组件时访问的子组件
<template>
<div>
<h3>这是首页Home组件</h3>
<p>
首页Home组件的内容 <br>
<router-link to="/home/news">新闻列表</router-link>
<router-link to="/home/messages">消息列表</router-link>
</p>
<router-view></router-view>
</div>
</template> <script>
export default {
name: "Home",
created() { },
data () {
return {
path : '/home/news'
}
},
activated() {
this.$router.push(this.path);
},
// deactivated() {
// this.path = this.$route.path;
// }
beforeRouteLeave (to, from, next) {
this.path = this.$route.path;
next();
}
}
</script> <style scoped> </style>
四、Keep-Alive的两个属性
<keep-alive
include="Home,Message"
exclude="News,Profile"
>
<router-view></router-view>
</keep-alive>
include表示需要缓存在里面的组件
exclude表示排除,不要缓存的组件
默认是使用正则表达式,符合正则规则的组件名称,就会把该组件选中
也可以是直接写组件的名称表示,注意不要有空格
组件的名称就是这个:
用途主要是为了排除特定不需要缓存的组件,一般需要缓存的不需要填写属性表示了
【Vue】Re18 Router 第五部分(KeepAlive)的更多相关文章
- Vue.js(23)之 keepAlive和activated
阅读: vue中前进刷新.后退缓存用户浏览数据和浏览位置的实践 keep-alive 组件级缓存 keep-alive <keep-alive> 包裹动态组件时,会缓存不活动的组件实例,而 ...
- 三、vue之router
三.vue之router 此时vue的脚手架.创建项目已经完成. ... vue的运行流程 index.html-->main.js-->App.vue-->router/index ...
- Vue中router两种传参方式
Vue中router两种传参方式 1.Vue中router使用query传参 相关Html: <!DOCTYPE html> <html lang="en"> ...
- 四 Vue学习 router学习
index.js: 按需加载组件: const login = r => require.ensure([], () => r(require('@/page/login')), 'log ...
- Vue基础系列(五)——Vue中的指令(中)
写在前面的话: 文章是个人学习过程中的总结,为方便以后回头在学习. 文章中会参考官方文档和其他的一些文章,示例均为亲自编写和实践,若有写的不对的地方欢迎大家和我一起交流. VUE基础系列目录 < ...
- vue 中router.go;router.push和router.replace的区别
vue 中router.go:router.push和router.replace的区别:https://blog.csdn.net/div_ma/article/details/79467165 t ...
- 【vue】 router.beforeEach
import store from '@/store' const Vue = require('vue') const Router = require('vue-router') Vue.use( ...
- vue & this.$router.resolve
vue & this.$router.resolve gotoAutoUpdate (query = {}) { const { href } = this.$router.resolve({ ...
- Vue中router路由的使用、router-link的使用(在项目中的实际运用方式)
文章目录 1.先看router中的index.js文件 2.router-link的使用 3.实现的效果 前提:router已经安装 1.先看router中的index.js文件 import Vue ...
- vue中router使用keep-alive缓存页面的注意事项
<keep-alive exclude="QRCode"> <router-view></router-view> </keep-aliv ...
随机推荐
- 你知道键盘是如何工作的吗?(xv6键盘驱动程序)
键盘驱动程序 公众号:Rand_cs 键盘如何工作的前文曾经说过,当时是以 Linux 0.11 为基础讲的但不系统,本文以 xv6 的键盘驱动程序为例来系统地讲述键盘是如何工作的.关于驱动程序前文磁 ...
- LeetCode 37. Sudoku Solver II 解数独 (C++/Java)
题目: Write a program to solve a Sudoku puzzle by filling the empty cells. A sudoku solution must sati ...
- WPS WORD EXCEL 不合并显示
WPS WORD EXCEL 不合并显示 版本:WPS 12 , 下载时间约是2023 年. 1.在开始菜单里找到 WPS OFFICE - 配置工具 2.点击"高级(A)". 3 ...
- asp.net上传Excel文件并读取内容,自定义上传控件样式
一.页面增加上传控件,并在上传时判断是否是Excel文件(根据后缀名判断): 1 <table> 2 <tr> 3 <td> 4 <span style=&q ...
- http的响应码200,404,302,500表示的含义分别是?
200 - 确定.客户端请求已成功 302 - 临时移动转移,请求的内容已临时移动新的位置 404 - 未找到文件或目录 500 - 服务器内部错误
- Java代码规范及异常汇总 非空异常 NullPointerException
Java规范及异常汇总1.java.lang.NullPointerException: nullorderReq.getId() != -1 修改为: orderReq.getId() != nul ...
- RIP总结
RIP 两种更新方式:定期更新和触发更新 管理距离为120,更新使用UDP520,更新周期30s,使用跳数作为度量值,最大15 RIP有三个版本RIPv1,RIPv2,RIPn ...
- (数据科学学习手札162)Python GIS神器geopandas 1.0版本发布
本文完整代码及附件已上传至我的Github仓库https://github.com/CNFeffery/DataScienceStudyNotes 1 简介 大家好我是费老师,就在昨天,Python生 ...
- 在Linux驱动中使用LED子系统
在Linux驱动中使用LED子系统 原文:https://blog.csdn.net/hanp_linux/article/details/79037684 前提配置device driver下面的L ...
- 基于SQLite3的C学习总结
背景 针对 SQLite3 的学习总结 arm linux 移植 SQLite 3 如何在 Linux 上移植使用 SQLite3,标题虽然是在讲 arm linux,但实际上是跨平台的. 基于 SQ ...