一、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)的更多相关文章

  1. Vue.js(23)之 keepAlive和activated

    阅读: vue中前进刷新.后退缓存用户浏览数据和浏览位置的实践 keep-alive 组件级缓存 keep-alive <keep-alive> 包裹动态组件时,会缓存不活动的组件实例,而 ...

  2. 三、vue之router

    三.vue之router 此时vue的脚手架.创建项目已经完成. ... vue的运行流程 index.html-->main.js-->App.vue-->router/index ...

  3. Vue中router两种传参方式

    Vue中router两种传参方式 1.Vue中router使用query传参 相关Html: <!DOCTYPE html> <html lang="en"> ...

  4. 四 Vue学习 router学习

    index.js: 按需加载组件: const login = r => require.ensure([], () => r(require('@/page/login')), 'log ...

  5. Vue基础系列(五)——Vue中的指令(中)

    写在前面的话: 文章是个人学习过程中的总结,为方便以后回头在学习. 文章中会参考官方文档和其他的一些文章,示例均为亲自编写和实践,若有写的不对的地方欢迎大家和我一起交流. VUE基础系列目录 < ...

  6. vue 中router.go;router.push和router.replace的区别

    vue 中router.go:router.push和router.replace的区别:https://blog.csdn.net/div_ma/article/details/79467165 t ...

  7. 【vue】 router.beforeEach

    import store from '@/store' const Vue = require('vue') const Router = require('vue-router') Vue.use( ...

  8. vue & this.$router.resolve

    vue & this.$router.resolve gotoAutoUpdate (query = {}) { const { href } = this.$router.resolve({ ...

  9. Vue中router路由的使用、router-link的使用(在项目中的实际运用方式)

    文章目录 1.先看router中的index.js文件 2.router-link的使用 3.实现的效果 前提:router已经安装 1.先看router中的index.js文件 import Vue ...

  10. vue中router使用keep-alive缓存页面的注意事项

    <keep-alive exclude="QRCode"> <router-view></router-view> </keep-aliv ...

随机推荐

  1. C#.NET6 ASP.NET CORE MVC 获取客户端IP

    重点是拿到HttpContext 对象. 先从Headers["Cdn-Src-Ip"] 中取IP,其次从Headers["X-Forwarded-For"]  ...

  2. asp.net core mvc 使用quartz

    参照了:https://www.cnblogs.com/dangzhensheng/p/10496278.html 1.新建任务类ReportJob.cs,这个类里就是具体任务了. using Qua ...

  3. 文件系统(六):一文看懂linux ext4文件系统工作原理

    liwen01 2024.06.09 前言 Linux系统中的ext2.ext3.ext4 文件系统,它们都有很强的向后和向前兼容性,可以在数据不丢失的情况下进行文件系统的升级.目前ext4是一个相对 ...

  4. Go 语言中的异常处理简单实践 panic、recover【GO 基础】

    〇.Go 中的异常处理简介 Golang 没有结构化异常,使用 panic 抛出错误,recover 捕获错误. panic.recover 参数类型为 interface{},因此可抛出任何类型对象 ...

  5. SQL索引优化,菜单列表优化

    SQL索引优化,菜单列表优化 现象:在系统中几个数据量大的列表页面,首次进入页面未增加筛选条件,导致进入的列表查询速度非常慢.分析:通过SQL查看,是做了count求和查询,然后根据总的记录数来做分页 ...

  6. 高级前端开发需要知道的 25 个 JavaScript 单行代码

    1. 不使用临时变量来交换变量的值 例如我们想要将 a 于 b 的值交换 let a = 1, b = 2; // 交换值 [a, b] = [b, a]; // 结果: a = 2, b = 1 这 ...

  7. MySQL Explain 关键字详解

    概述 explain 关键字可以模拟执行 sql 查询语句,输出执行计划,分析查询语句的执行性能 使用方式如下:explain + sql explain select * from t1 执行计划各 ...

  8. .htaccess伪静态规则

    Tips:当你看到这个提示的时候,说明当前的文章是由原emlog博客系统搬迁至此的,文章发布时间已过于久远,编排和内容不一定完整,还请谅解` .htaccess伪静态规则 日期:2017-12-4 阿 ...

  9. centos8使用nmcli实现bond

    #添加bonding接口 nmcli con add type bond con-name bond0 ifname bond0 mode active-backup ipv4.method manu ...

  10. injectionIII iOS代码注入工具(下)

    injectionIII iOS代码注入工具(下) 本文将解决如何使用injectionIII对主页热重载,如果对injectionIII不了解的同学请回到上篇查看 Vaccine 简单地说Vacci ...