一、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. 【Java】JVM字节码分析

    一.功能 1.工作原理 2.解释和运行 jvm本质上是运行在计算机上的程序,负责运行java字节码文件 对字节码文件中的指令,实时的解释成机器码,供计算机执行 3.内存管理 自动为对象.方法等分配内存 ...

  2. react做购物车的功能

    父组件 import React, { Component } from 'react' import Lists from '../components/Lists' export default ...

  3. vant做城市列表

    vant: https://youzan.github.io/vant/#/zh-CN/ 安装 cnpm i -S vant 按需加载配置 # 在 babel.config.js 中配置 module ...

  4. 讲课 PPT 公开啦

    目前限于时间原因,只在 Github Pages 上托管了. 之后有时间会托管到 pythonanywhere 上,因为 Github Pages 是在太慢了.

  5. SELinux(一) 简介

    首发公号:Rand_cs 前段时间的工作遇到了一些关于 SELinux 的问题,初次接触不熟悉此概念,导致当时配置策略时束手束脚,焦头烂额,为此去系统的学习了下 SELinux 的东西.聊 SELin ...

  6. MYSQL 连接数据库过程中发生错误,检查服务器是否正常连接字符串是否正确,错误信息:未将对象引用设置到对象的实例。

    一: 中文提示 : 连接数据库过程中发生错误,检查服务器是否正常连接字符串是否正确,错误信息:未将对象引用设置到对象的实例.DbType="MySql";ConfigId=&quo ...

  7. ubuntu server 安装慢 安装卡

    无论是桌面版本ubuntu,还是server 版本,都喜欢在安装过程中联网下东西: 默认的软件包镜像地址下载非常慢,你自身的网络再差点,可能会安装好几个小时. 解决方案: 方案1: 安装前拔网线. 方 ...

  8. Mysql常见使用问题的解决方法

    问题一:Mysql插入中文数据时,报错"incorrect string value"字符转换不正确 解决方法: 第一种方式: 1.更改Mysql安装目录下的文件my.ini(一般 ...

  9. 如何基于R包做GO分析?实现秒出图

    GO分析 基因本体论(Gene Ontology, GO)是一个用于描述基因和基因产品属性的标准术语体系.它提供了一个有组织的方式来表示基因在生物体内的各种角色.基因本体论通常从三个层面对基因进行描述 ...

  10. Mybatis if判断中使用了Ognl关键字导致报错解决方法

    mybatis xml中使用OGNL解析参数,如果直接使用了关键字则会导致解析失败. 常见的关键字有: 字段 mybatis关键字 bor (字符|)的英文 xor 字符^的英文 and 字符& ...