【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 ...
随机推荐
- xhs全参xs,xt,xscommon逆向分析
声明 本文章中所有内容仅供学习交流,抓包内容.敏感网址.数据接口均已做脱敏处理,严禁用于商业用途和非法用途,否则由此产生的一切后果均与作者无关,若有侵权,请联系我立即删除! 目标网站 aHR0cHM6 ...
- NCNN的内存显存分配器ncnn::Allocator & ncnn::VkAllocator翻译及其差异对比的学习笔记(nihui亲审过滴)
NCNN的内存分配器 ncnn::Allocator 通用内存分配器 ncnn::PoolAllocator 内存池分配器 可以设置池大小,减少分配内存和析构内存次数,空间换时间 ncnn:: ...
- LeetCode 460. LFU Cache LFU缓存 (C++/Java)
题目: Design and implement a data structure for Least Frequently Used (LFU)cache. It should support th ...
- nginx访问日志
访客日志 处理日志模块的官网教程 https://nginx.org/en/docs/http/ngx_http_log_module.html 创建nginx访问日志 日志对于程序员很重要,可用于问 ...
- 在线RSA公钥私钥生成工具
在线RSA非对称加密公钥私钥生成工具,提供便捷.安全的公私钥生成服务.支持多种密钥长度选择,满足个性化需求.一键生成PEM格式证书,让您快速实现数据加密与身份验证,保障数据安全,提升网络安全防护能力. ...
- @Async异步方法对异常的处理,从内层向外层抛出机制
@Async异步方法对异常的处理,从内层向外层抛出机制 @RequestMapping(value = "/test", method = RequestMethod.GET) p ...
- 安装Ingress-Nginx
目前,DHorse(https://gitee.com/i512team/dhorse)只支持Ingress-nginx的Ingress实现,下面介绍Ingress-nginx的安装过程. 下载安装文 ...
- RIP总结
RIP 两种更新方式:定期更新和触发更新 管理距离为120,更新使用UDP520,更新周期30s,使用跳数作为度量值,最大15 RIP有三个版本RIPv1,RIPv2,RIPn ...
- 浏览器中JS的执行
JS是在浏览器中运行的,浏览器为了运行JS, 必须要编译或解释JS,因为JS是高级语言,计算机不认识,必须把它编译或解释成机器语言,其次,在运行JS的过程,浏览器还要创建堆栈,因为程序是在栈中执行,执 ...
- vulhub - INFOSEC PREP: OSCP
vulhub - INFOSEC PREP: OSCP 信息收集 nmap 192.168.157.0/24 nmap -sT --min-rate 10000 -p- 192.168.157.162 ...